
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>高级类型 · TypeScript Handbook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Patrick Zhong">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Utilit Types.md" />
    
    
    <link rel="prev" href="Type Compatibility.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="tutorials/">
            
                <a href="tutorials/">
            
                    
                    快速上手
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="tutorials/TypeScript in 5 minutes.html">
            
                <a href="tutorials/TypeScript in 5 minutes.html">
            
                    
                    5分钟了解TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="tutorials/ASP.NET Core.html">
            
                <a href="tutorials/ASP.NET Core.html">
            
                    
                    ASP.NET Core
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="tutorials/ASP.NET 4.html">
            
                <a href="tutorials/ASP.NET 4.html">
            
                    
                    ASP.NET 4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="tutorials/Gulp.html">
            
                <a href="tutorials/Gulp.html">
            
                    
                    Gulp
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="tutorials/Knockout.html">
            
                <a href="tutorials/Knockout.html">
            
                    
                    Knockout.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="tutorials/React & Webpack.html">
            
                <a href="tutorials/React & Webpack.html">
            
                    
                    React与webpack
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="tutorials/React.html">
            
                <a href="tutorials/React.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="tutorials/Angular 2.html">
            
                <a href="tutorials/Angular 2.html">
            
                    
                    Angular 2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="tutorials/Migrating from JavaScript.html">
            
                <a href="tutorials/Migrating from JavaScript.html">
            
                    
                    从JavaScript迁移到TypeScript
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    手册
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Basic Types.html">
            
                <a href="Basic Types.html">
            
                    
                    基础类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="Variable Declarations.html">
            
                <a href="Variable Declarations.html">
            
                    
                    变量声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="Interfaces.html">
            
                <a href="Interfaces.html">
            
                    
                    接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="Classes.html">
            
                <a href="Classes.html">
            
                    
                    类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="Functions.html">
            
                <a href="Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="Generics.html">
            
                <a href="Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="Enums.html">
            
                <a href="Enums.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="Type Inference.html">
            
                <a href="Type Inference.html">
            
                    
                    类型推论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="Type Compatibility.html">
            
                <a href="Type Compatibility.html">
            
                    
                    类型兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.10" data-path="Advanced Types.html">
            
                <a href="Advanced Types.html">
            
                    
                    高级类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="Utilit Types.md">
            
                <span>
            
                    
                    实用工具类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="Symbols.html">
            
                <a href="Symbols.html">
            
                    
                    Symbols
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="Iterators and Generators.html">
            
                <a href="Iterators and Generators.html">
            
                    
                    Iterators 和 Generators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="Modules.html">
            
                <a href="Modules.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="Namespaces.html">
            
                <a href="Namespaces.html">
            
                    
                    命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="Namespaces and Modules.html">
            
                <a href="Namespaces and Modules.html">
            
                    
                    命名空间和模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="Module Resolution.html">
            
                <a href="Module Resolution.html">
            
                    
                    模块解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="Declaration Merging.html">
            
                <a href="Declaration Merging.html">
            
                    
                    声明合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="Writing Definition Files.html">
            
                <a href="Writing Definition Files.html">
            
                    
                    书写.d.ts文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="JSX.html">
            
                <a href="JSX.html">
            
                    
                    JSX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="Decorators.html">
            
                <a href="Decorators.html">
            
                    
                    Decorators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="Mixins.html">
            
                <a href="Mixins.html">
            
                    
                    混入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="Triple-Slash Directives.html">
            
                <a href="Triple-Slash Directives.html">
            
                    
                    三斜线指令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="Type Checking JavaScript Files.html">
            
                <a href="Type Checking JavaScript Files.html">
            
                    
                    JavaScript文件里的类型检查
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="declaration files/Introduction.html">
            
                <a href="declaration files/Introduction.html">
            
                    
                    如何书写声明文件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="declaration files/Library Structures.html">
            
                <a href="declaration files/Library Structures.html">
            
                    
                    结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="declaration files/Do's and Don'ts.html">
            
                <a href="declaration files/Do's and Don'ts.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="declaration files/By Example.html">
            
                <a href="declaration files/By Example.html">
            
                    
                    举例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="declaration files/Deep Dive.html">
            
                <a href="declaration files/Deep Dive.html">
            
                    
                    深入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="declaration files/Publishing.html">
            
                <a href="declaration files/Publishing.html">
            
                    
                    发布
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="declaration files/Consumption.html">
            
                <a href="declaration files/Consumption.html">
            
                    
                    使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="tsconfig.json.html">
            
                <a href="tsconfig.json.html">
            
                    
                    工程配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="tsconfig.json.html">
            
                <a href="tsconfig.json.html">
            
                    
                    tsconfig.json
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="Project References.html">
            
                <a href="Project References.html">
            
                    
                    工程引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="Typings for NPM Packages.html">
            
                <a href="Typings for NPM Packages.html">
            
                    
                    NPM包的类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="Compiler Options.html">
            
                <a href="Compiler Options.html">
            
                    
                    编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="Configuring Watch.html">
            
                <a href="Configuring Watch.html">
            
                    
                    配置 Watch
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="Compiler Options in MSBuild.html">
            
                <a href="Compiler Options in MSBuild.html">
            
                    
                    在MSBuild里使用编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="Integrating with Build Tools.html">
            
                <a href="Integrating with Build Tools.html">
            
                    
                    与其它构建工具整合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="Nightly Builds.html">
            
                <a href="Nightly Builds.html">
            
                    
                    使用TypeScript的每日构建版本
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../wiki/">
            
                <a href="../wiki/">
            
                    
                    Wiki
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../wiki/this-in-TypeScript.html">
            
                <a href="../wiki/this-in-TypeScript.html">
            
                    
                    TypeScript里的this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../wiki/coding_guidelines.html">
            
                <a href="../wiki/coding_guidelines.html">
            
                    
                    编码规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../wiki/Common Errors.html">
            
                <a href="../wiki/Common Errors.html">
            
                    
                    常见编译错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../wiki/TypeScript-Editor-Support.html">
            
                <a href="../wiki/TypeScript-Editor-Support.html">
            
                    
                    支持TypeScript的编辑器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                <a href="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                    
                    结合ASP.NET v5使用TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../wiki/Architectural-Overview.html">
            
                <a href="../wiki/Architectural-Overview.html">
            
                    
                    架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../wiki/Roadmap.html">
            
                <a href="../wiki/Roadmap.html">
            
                    
                    发展路线图
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../release-notes/">
            
                <a href="../release-notes/">
            
                    
                    新增功能
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../release-notes/TypeScript 3.1.html">
            
                <a href="../release-notes/TypeScript 3.1.html">
            
                    
                    TypeScript 3.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../release-notes/TypeScript 3.0.html">
            
                <a href="../release-notes/TypeScript 3.0.html">
            
                    
                    TypeScript 3.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../release-notes/TypeScript 2.9.html">
            
                <a href="../release-notes/TypeScript 2.9.html">
            
                    
                    TypeScript 2.9
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../release-notes/TypeScript 2.8.html">
            
                <a href="../release-notes/TypeScript 2.8.html">
            
                    
                    TypeScript 2.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../release-notes/TypeScript 2.7.html">
            
                <a href="../release-notes/TypeScript 2.7.html">
            
                    
                    TypeScript 2.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../release-notes/TypeScript 2.6.html">
            
                <a href="../release-notes/TypeScript 2.6.html">
            
                    
                    TypeScript 2.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../release-notes/TypeScript 2.5.html">
            
                <a href="../release-notes/TypeScript 2.5.html">
            
                    
                    TypeScript 2.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="../release-notes/TypeScript 2.4.html">
            
                <a href="../release-notes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="../release-notes/TypeScript 2.3.html">
            
                <a href="../release-notes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="../release-notes/TypeScript 2.2.html">
            
                <a href="../release-notes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.11" data-path="../release-notes/TypeScript 2.1.html">
            
                <a href="../release-notes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.12" data-path="../release-notes/TypeScript 2.0.html">
            
                <a href="../release-notes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.13" data-path="../release-notes/TypeScript 1.8.html">
            
                <a href="../release-notes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.14" data-path="../release-notes/TypeScript 1.7.html">
            
                <a href="../release-notes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.15" data-path="../release-notes/TypeScript 1.6.html">
            
                <a href="../release-notes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.16" data-path="../release-notes/TypeScript 1.5.html">
            
                <a href="../release-notes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.17" data-path="../release-notes/TypeScript 1.4.html">
            
                <a href="../release-notes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.18" data-path="../release-notes/TypeScript 1.3.html">
            
                <a href="../release-notes/TypeScript 1.3.html">
            
                    
                    TypeScript 1.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.19" data-path="../release-notes/TypeScript 1.1.html">
            
                <a href="../release-notes/TypeScript 1.1.html">
            
                    
                    TypeScript 1.1
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../breaking-changes/breaking-changes.html">
            
                <a href="../breaking-changes/breaking-changes.html">
            
                    
                    Breaking Changes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../breaking-changes/TypeScript 2.4.html">
            
                <a href="../breaking-changes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../breaking-changes/TypeScript 2.3.html">
            
                <a href="../breaking-changes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../breaking-changes/TypeScript 2.2.html">
            
                <a href="../breaking-changes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../breaking-changes/TypeScript 2.1.html">
            
                <a href="../breaking-changes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../breaking-changes/TypeScript 2.0.html">
            
                <a href="../breaking-changes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../breaking-changes/TypeScript 1.8.html">
            
                <a href="../breaking-changes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../breaking-changes/TypeScript 1.7.html">
            
                <a href="../breaking-changes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../breaking-changes/TypeScript 1.6.html">
            
                <a href="../breaking-changes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../breaking-changes/TypeScript 1.5.html">
            
                <a href="../breaking-changes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.10" data-path="../breaking-changes/TypeScript 1.4.html">
            
                <a href="../breaking-changes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >高级类型</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x4EA4;&#x53C9;&#x7C7B;&#x578B;&#xFF08;intersection-types&#xFF09;">&#x4EA4;&#x53C9;&#x7C7B;&#x578B;&#xFF08;Intersection Types&#xFF09;</h1>
<p>&#x4EA4;&#x53C9;&#x7C7B;&#x578B;&#x662F;&#x5C06;&#x591A;&#x4E2A;&#x7C7B;&#x578B;&#x5408;&#x5E76;&#x4E3A;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x3002;
&#x8FD9;&#x8BA9;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A;&#x73B0;&#x6709;&#x7684;&#x591A;&#x79CD;&#x7C7B;&#x578B;&#x53E0;&#x52A0;&#x5230;&#x4E00;&#x8D77;&#x6210;&#x4E3A;&#x4E00;&#x79CD;&#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x5305;&#x542B;&#x4E86;&#x6240;&#x9700;&#x7684;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x7279;&#x6027;&#x3002;
&#x4F8B;&#x5982;&#xFF0C;<code>Person &amp; Serializable &amp; Loggable</code>&#x540C;&#x65F6;&#x662F;<code>Person</code><em>&#x548C;</em><code>Serializable</code><em>&#x548C;</em><code>Loggable</code>&#x3002;
&#x5C31;&#x662F;&#x8BF4;&#x8FD9;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x5BF9;&#x8C61;&#x540C;&#x65F6;&#x62E5;&#x6709;&#x4E86;&#x8FD9;&#x4E09;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x6210;&#x5458;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5927;&#x591A;&#x662F;&#x5728;&#x6DF7;&#x5165;&#xFF08;mixins&#xFF09;&#x6216;&#x5176;&#x5B83;&#x4E0D;&#x9002;&#x5408;&#x5178;&#x578B;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x6A21;&#x578B;&#x7684;&#x5730;&#x65B9;&#x770B;&#x5230;&#x4EA4;&#x53C9;&#x7C7B;&#x578B;&#x7684;&#x4F7F;&#x7528;&#x3002;
&#xFF08;&#x5728;JavaScript&#x91CC;&#x53D1;&#x751F;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x7684;&#x573A;&#x5408;&#x5F88;&#x591A;&#xFF01;&#xFF09;
&#x4E0B;&#x9762;&#x662F;&#x5982;&#x4F55;&#x521B;&#x5EFA;&#x6DF7;&#x5165;&#x7684;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x4F8B;&#x5B50;(&quot;target&quot;: &quot;es5&quot;)&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">extend</span>&lt;<span class="hljs-title">T</span>, <span class="hljs-title">U</span>&gt;(<span class="hljs-params">first: T, second: U</span>): <span class="hljs-title">T</span> &amp; <span class="hljs-title">U</span> </span>{
    <span class="hljs-keyword">let</span> result = &lt;T &amp; U&gt;{};
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> id <span class="hljs-keyword">in</span> first) {
        (&lt;<span class="hljs-built_in">any</span>&gt;result)[id] = (&lt;<span class="hljs-built_in">any</span>&gt;first)[id];
    }
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> id <span class="hljs-keyword">in</span> second) {
        <span class="hljs-keyword">if</span> (!result.hasOwnProperty(id)) {
            (&lt;<span class="hljs-built_in">any</span>&gt;result)[id] = (&lt;<span class="hljs-built_in">any</span>&gt;second)[id];
        }
    }
    <span class="hljs-keyword">return</span> result;
}

<span class="hljs-keyword">class</span> Person {
    <span class="hljs-keyword">constructor</span>(public name: string) { }
}
<span class="hljs-keyword">interface</span> Loggable {
    log(): <span class="hljs-built_in">void</span>;
}
<span class="hljs-keyword">class</span> ConsoleLogger <span class="hljs-keyword">implements</span> Loggable {
    log() {
        <span class="hljs-comment">// ...</span>
    }
}
<span class="hljs-keyword">var</span> jim = extend(<span class="hljs-keyword">new</span> Person(<span class="hljs-string">&quot;Jim&quot;</span>), <span class="hljs-keyword">new</span> ConsoleLogger());
<span class="hljs-keyword">var</span> n = jim.name;
jim.log();
</code></pre>
<h1 id="&#x8054;&#x5408;&#x7C7B;&#x578B;&#xFF08;union-types&#xFF09;">&#x8054;&#x5408;&#x7C7B;&#x578B;&#xFF08;Union Types&#xFF09;</h1>
<p>&#x8054;&#x5408;&#x7C7B;&#x578B;&#x4E0E;&#x4EA4;&#x53C9;&#x7C7B;&#x578B;&#x5F88;&#x6709;&#x5173;&#x8054;&#xFF0C;&#x4F46;&#x662F;&#x4F7F;&#x7528;&#x4E0A;&#x5374;&#x5B8C;&#x5168;&#x4E0D;&#x540C;&#x3002;
&#x5076;&#x5C14;&#x4F60;&#x4F1A;&#x9047;&#x5230;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x5E93;&#x5E0C;&#x671B;&#x4F20;&#x5165;<code>number</code>&#x6216;<code>string</code>&#x7C7B;&#x578B;&#x7684;&#x53C2;&#x6570;&#x3002;
&#x4F8B;&#x5982;&#x4E0B;&#x9762;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-comment">/**
 * Takes a string and adds &quot;padding&quot; to the left.
 * If &apos;padding&apos; is a string, then &apos;padding&apos; is appended to the left side.
 * If &apos;padding&apos; is a number, then that number of spaces is added to the left side.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">padLeft</span>(<span class="hljs-params">value: <span class="hljs-built_in">string</span>, padding: <span class="hljs-built_in">any</span></span>) </span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> padding === <span class="hljs-string">&quot;number&quot;</span>) {
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">Array</span>(padding + <span class="hljs-number">1</span>).join(<span class="hljs-string">&quot; &quot;</span>) + value;
    }
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> padding === <span class="hljs-string">&quot;string&quot;</span>) {
        <span class="hljs-keyword">return</span> padding + value;
    }
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">`Expected string or number, got &apos;<span class="hljs-subst">${padding}</span>&apos;.`</span>);
}

padLeft(<span class="hljs-string">&quot;Hello world&quot;</span>, <span class="hljs-number">4</span>); <span class="hljs-comment">// returns &quot;    Hello world&quot;</span>
</code></pre>
<p><code>padLeft</code>&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;<code>padding</code>&#x53C2;&#x6570;&#x7684;&#x7C7B;&#x578B;&#x6307;&#x5B9A;&#x6210;&#x4E86;<code>any</code>&#x3002;
&#x8FD9;&#x5C31;&#x662F;&#x8BF4;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x65E2;&#x4E0D;&#x662F;<code>number</code>&#x4E5F;&#x4E0D;&#x662F;<code>string</code>&#x7C7B;&#x578B;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x4F46;&#x662F;TypeScript&#x5374;&#x4E0D;&#x62A5;&#x9519;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> indentedString = padLeft(<span class="hljs-string">&quot;Hello world&quot;</span>, <span class="hljs-literal">true</span>); <span class="hljs-comment">// &#x7F16;&#x8BD1;&#x9636;&#x6BB5;&#x901A;&#x8FC7;&#xFF0C;&#x8FD0;&#x884C;&#x65F6;&#x62A5;&#x9519;</span>
</code></pre>
<p>&#x5728;&#x4F20;&#x7EDF;&#x7684;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x8BED;&#x8A00;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x80FD;&#x4F1A;&#x5C06;&#x8FD9;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#x62BD;&#x8C61;&#x6210;&#x6709;&#x5C42;&#x7EA7;&#x7684;&#x7C7B;&#x578B;&#x3002;
&#x8FD9;&#x4E48;&#x505A;&#x663E;&#x7136;&#x662F;&#x975E;&#x5E38;&#x6E05;&#x6670;&#x7684;&#xFF0C;&#x4F46;&#x540C;&#x65F6;&#x4E5F;&#x5B58;&#x5728;&#x4E86;&#x8FC7;&#x5EA6;&#x8BBE;&#x8BA1;&#x3002;
<code>padLeft</code>&#x539F;&#x59CB;&#x7248;&#x672C;&#x7684;&#x597D;&#x5904;&#x4E4B;&#x4E00;&#x662F;&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x4F20;&#x5165;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x3002;
&#x8FD9;&#x6837;&#x505A;&#x7684;&#x8BDD;&#x4F7F;&#x7528;&#x8D77;&#x6765;&#x65E2;&#x7B80;&#x5355;&#x53C8;&#x65B9;&#x4FBF;&#x3002;
&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5C31;&#x662F;&#x60F3;&#x4F7F;&#x7528;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684;&#x51FD;&#x6570;&#x7684;&#x8BDD;&#xFF0C;&#x8FD9;&#x79CD;&#x65B0;&#x7684;&#x65B9;&#x5F0F;&#x5C31;&#x4E0D;&#x9002;&#x7528;&#x4E86;&#x3002;</p>
<p>&#x4EE3;&#x66FF;<code>any</code>&#xFF0C; &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<em>&#x8054;&#x5408;&#x7C7B;&#x578B;</em>&#x505A;&#x4E3A;<code>padding</code>&#x7684;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-comment">/**
 * Takes a string and adds &quot;padding&quot; to the left.
 * If &apos;padding&apos; is a string, then &apos;padding&apos; is appended to the left side.
 * If &apos;padding&apos; is a number, then that number of spaces is added to the left side.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">padLeft</span>(<span class="hljs-params">value: <span class="hljs-built_in">string</span>, padding: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span></span>) </span>{
    <span class="hljs-comment">// ...</span>
}

<span class="hljs-keyword">let</span> indentedString = padLeft(<span class="hljs-string">&quot;Hello world&quot;</span>, <span class="hljs-literal">true</span>); <span class="hljs-comment">// errors during compilation</span>
</code></pre>
<p>&#x8054;&#x5408;&#x7C7B;&#x578B;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x503C;&#x53EF;&#x4EE5;&#x662F;&#x51E0;&#x79CD;&#x7C7B;&#x578B;&#x4E4B;&#x4E00;&#x3002;
&#x6211;&#x4EEC;&#x7528;&#x7AD6;&#x7EBF;&#xFF08;<code>|</code>&#xFF09;&#x5206;&#x9694;&#x6BCF;&#x4E2A;&#x7C7B;&#x578B;&#xFF0C;&#x6240;&#x4EE5;<code>number | string | boolean</code>&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x503C;&#x53EF;&#x4EE5;&#x662F;<code>number</code>&#xFF0C;<code>string</code>&#xFF0C;&#x6216;<code>boolean</code>&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x503C;&#x662F;&#x8054;&#x5408;&#x7C7B;&#x578B;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x80FD;&#x8BBF;&#x95EE;&#x6B64;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x91CC;&#x5171;&#x6709;&#x7684;&#x6210;&#x5458;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Bird {
    fly();
    layEggs();
}

<span class="hljs-keyword">interface</span> Fish {
    swim();
    layEggs();
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getSmallPet</span>(<span class="hljs-params"></span>): <span class="hljs-title">Fish</span> | <span class="hljs-title">Bird</span> </span>{
    <span class="hljs-comment">// ...</span>
}

<span class="hljs-keyword">let</span> pet = getSmallPet();
pet.layEggs(); <span class="hljs-comment">// okay</span>
pet.swim();    <span class="hljs-comment">// errors</span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x53EF;&#x80FD;&#x6709;&#x70B9;&#x590D;&#x6742;&#xFF0C;&#x4F46;&#x662F;&#x4F60;&#x5F88;&#x5BB9;&#x6613;&#x5C31;&#x4E60;&#x60EF;&#x4E86;&#x3002;
&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x662F;<code>A | B</code>&#xFF0C;&#x6211;&#x4EEC;&#x80FD;&#x591F;<em>&#x786E;&#x5B9A;</em>&#x7684;&#x662F;&#x5B83;&#x5305;&#x542B;&#x4E86;<code>A</code><em>&#x548C;</em><code>B</code>&#x4E2D;&#x5171;&#x6709;&#x7684;&#x6210;&#x5458;&#x3002;
&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;<code>Bird</code>&#x5177;&#x6709;&#x4E00;&#x4E2A;<code>fly</code>&#x6210;&#x5458;&#x3002;
&#x6211;&#x4EEC;&#x4E0D;&#x80FD;&#x786E;&#x5B9A;&#x4E00;&#x4E2A;<code>Bird | Fish</code>&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x662F;&#x5426;&#x6709;<code>fly</code>&#x65B9;&#x6CD5;&#x3002;
&#x5982;&#x679C;&#x53D8;&#x91CF;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x662F;<code>Fish</code>&#x7C7B;&#x578B;&#xFF0C;&#x90A3;&#x4E48;&#x8C03;&#x7528;<code>pet.fly()</code>&#x5C31;&#x51FA;&#x9519;&#x4E86;&#x3002;</p>
<h1 id="&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x4E0E;&#x533A;&#x5206;&#x7C7B;&#x578B;&#xFF08;type-guards-and-differentiating-types&#xFF09;">&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x4E0E;&#x533A;&#x5206;&#x7C7B;&#x578B;&#xFF08;Type Guards and Differentiating Types&#xFF09;</h1>
<p>&#x8054;&#x5408;&#x7C7B;&#x578B;&#x9002;&#x5408;&#x4E8E;&#x90A3;&#x4E9B;&#x503C;&#x53EF;&#x4EE5;&#x4E3A;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x60C5;&#x51B5;&#x3002;
&#x4F46;&#x5F53;&#x6211;&#x4EEC;&#x60F3;&#x786E;&#x5207;&#x5730;&#x4E86;&#x89E3;&#x662F;&#x5426;&#x4E3A;<code>Fish</code>&#x65F6;&#x600E;&#x4E48;&#x529E;&#xFF1F;
JavaScript&#x91CC;&#x5E38;&#x7528;&#x6765;&#x533A;&#x5206;2&#x4E2A;&#x53EF;&#x80FD;&#x503C;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x68C0;&#x67E5;&#x6210;&#x5458;&#x662F;&#x5426;&#x5B58;&#x5728;&#x3002;
&#x5982;&#x4E4B;&#x524D;&#x63D0;&#x53CA;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x80FD;&#x8BBF;&#x95EE;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x4E2D;&#x5171;&#x540C;&#x62E5;&#x6709;&#x7684;&#x6210;&#x5458;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> pet = getSmallPet();

<span class="hljs-comment">// &#x6BCF;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x8BBF;&#x95EE;&#x90FD;&#x4F1A;&#x62A5;&#x9519;</span>
<span class="hljs-keyword">if</span> (pet.swim) {
    pet.swim();
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (pet.fly) {
    pet.fly();
}
</code></pre>
<p>&#x4E3A;&#x4E86;&#x8BA9;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x5DE5;&#x4F5C;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x65AD;&#x8A00;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> pet = getSmallPet();

<span class="hljs-keyword">if</span> ((&lt;Fish&gt;pet).swim) {
    (&lt;Fish&gt;pet).swim();
}
<span class="hljs-keyword">else</span> {
    (&lt;Bird&gt;pet).fly();
}
</code></pre>
<h2 id="&#x7528;&#x6237;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;">&#x7528;&#x6237;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;</h2>
<p>&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x6CE8;&#x610F;&#x5230;&#x6211;&#x4EEC;&#x4E0D;&#x5F97;&#x4E0D;&#x591A;&#x6B21;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x65AD;&#x8A00;&#x3002;
&#x5047;&#x82E5;&#x6211;&#x4EEC;&#x4E00;&#x65E6;&#x68C0;&#x67E5;&#x8FC7;&#x7C7B;&#x578B;&#xFF0C;&#x5C31;&#x80FD;&#x5728;&#x4E4B;&#x540E;&#x7684;&#x6BCF;&#x4E2A;&#x5206;&#x652F;&#x91CC;&#x6E05;&#x695A;&#x5730;&#x77E5;&#x9053;<code>pet</code>&#x7684;&#x7C7B;&#x578B;&#x7684;&#x8BDD;&#x5C31;&#x597D;&#x4E86;&#x3002;</p>
<p>TypeScript&#x91CC;&#x7684;<em>&#x7C7B;&#x578B;&#x4FDD;&#x62A4;</em>&#x673A;&#x5236;&#x8BA9;&#x5B83;&#x6210;&#x4E3A;&#x4E86;&#x73B0;&#x5B9E;&#x3002;
&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x5C31;&#x662F;&#x4E00;&#x4E9B;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x5B83;&#x4EEC;&#x4F1A;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x68C0;&#x67E5;&#x4EE5;&#x786E;&#x4FDD;&#x5728;&#x67D0;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x91CC;&#x7684;&#x7C7B;&#x578B;&#x3002;
&#x8981;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x8981;&#x7B80;&#x5355;&#x5730;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E00;&#x4E2A;<em>&#x7C7B;&#x578B;&#x8C13;&#x8BCD;</em>&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isFish</span>(<span class="hljs-params">pet: Fish | Bird</span>): <span class="hljs-title">pet</span> <span class="hljs-title">is</span> <span class="hljs-title">Fish</span> </span>{
    <span class="hljs-keyword">return</span> (&lt;Fish&gt;pet).swim !== <span class="hljs-literal">undefined</span>;
}
</code></pre>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;<code>pet is Fish</code>&#x5C31;&#x662F;&#x7C7B;&#x578B;&#x8C13;&#x8BCD;&#x3002;
&#x8C13;&#x8BCD;&#x4E3A;<code>parameterName is Type</code>&#x8FD9;&#x79CD;&#x5F62;&#x5F0F;&#xFF0C;<code>parameterName</code>&#x5FC5;&#x987B;&#x662F;&#x6765;&#x81EA;&#x4E8E;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x7B7E;&#x540D;&#x91CC;&#x7684;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x540D;&#x3002;</p>
<p>&#x6BCF;&#x5F53;&#x4F7F;&#x7528;&#x4E00;&#x4E9B;&#x53D8;&#x91CF;&#x8C03;&#x7528;<code>isFish</code>&#x65F6;&#xFF0C;TypeScript&#x4F1A;&#x5C06;&#x53D8;&#x91CF;&#x7F29;&#x51CF;&#x4E3A;&#x90A3;&#x4E2A;&#x5177;&#x4F53;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x53EA;&#x8981;&#x8FD9;&#x4E2A;&#x7C7B;&#x578B;&#x4E0E;&#x53D8;&#x91CF;&#x7684;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x662F;&#x517C;&#x5BB9;&#x7684;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// &apos;swim&apos; &#x548C; &apos;fly&apos; &#x8C03;&#x7528;&#x90FD;&#x6CA1;&#x6709;&#x95EE;&#x9898;&#x4E86;</span>

<span class="hljs-keyword">if</span> (isFish(pet)) {
    pet.swim();
}
<span class="hljs-keyword">else</span> {
    pet.fly();
}
</code></pre>
<p>&#x6CE8;&#x610F;TypeScript&#x4E0D;&#x4EC5;&#x77E5;&#x9053;&#x5728;<code>if</code>&#x5206;&#x652F;&#x91CC;<code>pet</code>&#x662F;<code>Fish</code>&#x7C7B;&#x578B;&#xFF1B;
&#x5B83;&#x8FD8;&#x6E05;&#x695A;&#x5728;<code>else</code>&#x5206;&#x652F;&#x91CC;&#xFF0C;&#x4E00;&#x5B9A;<em>&#x4E0D;&#x662F;</em><code>Fish</code>&#x7C7B;&#x578B;&#xFF0C;&#x4E00;&#x5B9A;&#x662F;<code>Bird</code>&#x7C7B;&#x578B;&#x3002;</p>
<h2 id="typeof&#x7C7B;&#x578B;&#x4FDD;&#x62A4;"><code>typeof</code>&#x7C7B;&#x578B;&#x4FDD;&#x62A4;</h2>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x56DE;&#x8FC7;&#x5934;&#x6765;&#x770B;&#x770B;&#x600E;&#x4E48;&#x4F7F;&#x7528;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x4E66;&#x5199;<code>padLeft</code>&#x4EE3;&#x7801;&#x3002;
&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x5229;&#x7528;&#x7C7B;&#x578B;&#x65AD;&#x8A00;&#x6765;&#x5199;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isNumber</span>(<span class="hljs-params">x: <span class="hljs-built_in">any</span></span>): <span class="hljs-title">x</span> <span class="hljs-title">is</span> <span class="hljs-title">number</span> </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">typeof</span> x === <span class="hljs-string">&quot;number&quot;</span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isString</span>(<span class="hljs-params">x: <span class="hljs-built_in">any</span></span>): <span class="hljs-title">x</span> <span class="hljs-title">is</span> <span class="hljs-title">string</span> </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">typeof</span> x === <span class="hljs-string">&quot;string&quot;</span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">padLeft</span>(<span class="hljs-params">value: <span class="hljs-built_in">string</span>, padding: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span></span>) </span>{
    <span class="hljs-keyword">if</span> (isNumber(padding)) {
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">Array</span>(padding + <span class="hljs-number">1</span>).join(<span class="hljs-string">&quot; &quot;</span>) + value;
    }
    <span class="hljs-keyword">if</span> (isString(padding)) {
        <span class="hljs-keyword">return</span> padding + value;
    }
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">`Expected string or number, got &apos;<span class="hljs-subst">${padding}</span>&apos;.`</span>);
}
</code></pre>
<p>&#x7136;&#x800C;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6765;&#x5224;&#x65AD;&#x7C7B;&#x578B;&#x662F;&#x5426;&#x662F;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x592A;&#x75DB;&#x82E6;&#x4E86;&#x3002;
&#x5E78;&#x8FD0;&#x7684;&#x662F;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x4E0D;&#x5FC5;&#x5C06;<code>typeof x === &quot;number&quot;</code>&#x62BD;&#x8C61;&#x6210;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x56E0;&#x4E3A;TypeScript&#x53EF;&#x4EE5;&#x5C06;&#x5B83;&#x8BC6;&#x522B;&#x4E3A;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x3002;
&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728;&#x4EE3;&#x7801;&#x91CC;&#x68C0;&#x67E5;&#x7C7B;&#x578B;&#x4E86;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">padLeft</span>(<span class="hljs-params">value: <span class="hljs-built_in">string</span>, padding: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span></span>) </span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> padding === <span class="hljs-string">&quot;number&quot;</span>) {
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">Array</span>(padding + <span class="hljs-number">1</span>).join(<span class="hljs-string">&quot; &quot;</span>) + value;
    }
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> padding === <span class="hljs-string">&quot;string&quot;</span>) {
        <span class="hljs-keyword">return</span> padding + value;
    }
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">`Expected string or number, got &apos;<span class="hljs-subst">${padding}</span>&apos;.`</span>);
}
</code></pre>
<p>&#x8FD9;&#x4E9B;<em><code>typeof</code>&#x7C7B;&#x578B;&#x4FDD;&#x62A4;</em>&#x53EA;&#x6709;&#x4E24;&#x79CD;&#x5F62;&#x5F0F;&#x80FD;&#x88AB;&#x8BC6;&#x522B;&#xFF1A;<code>typeof v === &quot;typename&quot;</code>&#x548C;<code>typeof v !== &quot;typename&quot;</code>&#xFF0C;<code>&quot;typename&quot;</code>&#x5FC5;&#x987B;&#x662F;<code>&quot;number&quot;</code>&#xFF0C;<code>&quot;string&quot;</code>&#xFF0C;<code>&quot;boolean&quot;</code>&#x6216;<code>&quot;symbol&quot;</code>&#x3002;
&#x4F46;&#x662F;TypeScript&#x5E76;&#x4E0D;&#x4F1A;&#x963B;&#x6B62;&#x4F60;&#x4E0E;&#x5176;&#x5B83;&#x5B57;&#x7B26;&#x4E32;&#x6BD4;&#x8F83;&#xFF0C;&#x8BED;&#x8A00;&#x4E0D;&#x4F1A;&#x628A;&#x90A3;&#x4E9B;&#x8868;&#x8FBE;&#x5F0F;&#x8BC6;&#x522B;&#x4E3A;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x3002;</p>
<h2 id="instanceof&#x7C7B;&#x578B;&#x4FDD;&#x62A4;"><code>instanceof</code>&#x7C7B;&#x578B;&#x4FDD;&#x62A4;</h2>
<p>&#x5982;&#x679C;&#x4F60;&#x5DF2;&#x7ECF;&#x9605;&#x8BFB;&#x4E86;<code>typeof</code>&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x5E76;&#x4E14;&#x5BF9;JavaScript&#x91CC;&#x7684;<code>instanceof</code>&#x64CD;&#x4F5C;&#x7B26;&#x719F;&#x6089;&#x7684;&#x8BDD;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x5DF2;&#x7ECF;&#x731C;&#x5230;&#x4E86;&#x8FD9;&#x8282;&#x8981;&#x8BB2;&#x7684;&#x5185;&#x5BB9;&#x3002;</p>
<p><em><code>instanceof</code>&#x7C7B;&#x578B;&#x4FDD;&#x62A4;</em>&#x662F;&#x901A;&#x8FC7;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6765;&#x7EC6;&#x5316;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x3002;
&#x6BD4;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x501F;&#x9274;&#x4E00;&#x4E0B;&#x4E4B;&#x524D;&#x5B57;&#x7B26;&#x4E32;&#x586B;&#x5145;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Padder {
    getPaddingString(): <span class="hljs-built_in">string</span>
}

<span class="hljs-keyword">class</span> SpaceRepeatingPadder <span class="hljs-keyword">implements</span> Padder {
    <span class="hljs-keyword">constructor</span>(private numSpaces: number) { }
    getPaddingString() {
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">Array</span>(<span class="hljs-keyword">this</span>.numSpaces + <span class="hljs-number">1</span>).join(<span class="hljs-string">&quot; &quot;</span>);
    }
}

<span class="hljs-keyword">class</span> StringPadder <span class="hljs-keyword">implements</span> Padder {
    <span class="hljs-keyword">constructor</span>(private value: string) { }
    getPaddingString() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.value;
    }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getRandomPadder</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.random() &lt; <span class="hljs-number">0.5</span> ?
        <span class="hljs-keyword">new</span> SpaceRepeatingPadder(<span class="hljs-number">4</span>) :
        <span class="hljs-keyword">new</span> StringPadder(<span class="hljs-string">&quot;  &quot;</span>);
}

<span class="hljs-comment">// &#x7C7B;&#x578B;&#x4E3A;SpaceRepeatingPadder | StringPadder</span>
<span class="hljs-keyword">let</span> padder: Padder = getRandomPadder();

<span class="hljs-keyword">if</span> (padder <span class="hljs-keyword">instanceof</span> SpaceRepeatingPadder) {
    padder; <span class="hljs-comment">// &#x7C7B;&#x578B;&#x7EC6;&#x5316;&#x4E3A;&apos;SpaceRepeatingPadder&apos;</span>
}
<span class="hljs-keyword">if</span> (padder <span class="hljs-keyword">instanceof</span> StringPadder) {
    padder; <span class="hljs-comment">// &#x7C7B;&#x578B;&#x7EC6;&#x5316;&#x4E3A;&apos;StringPadder&apos;</span>
}
</code></pre>
<p><code>instanceof</code>&#x7684;&#x53F3;&#x4FA7;&#x8981;&#x6C42;&#x662F;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;TypeScript&#x5C06;&#x7EC6;&#x5316;&#x4E3A;&#xFF1A;</p>
<ol>
<li>&#x6B64;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;<code>prototype</code>&#x5C5E;&#x6027;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x5982;&#x679C;&#x5B83;&#x7684;&#x7C7B;&#x578B;&#x4E0D;&#x4E3A;<code>any</code>&#x7684;&#x8BDD;</li>
<li>&#x6784;&#x9020;&#x7B7E;&#x540D;&#x6240;&#x8FD4;&#x56DE;&#x7684;&#x7C7B;&#x578B;&#x7684;&#x8054;&#x5408;</li>
</ol>
<p>&#x4EE5;&#x6B64;&#x987A;&#x5E8F;&#x3002;</p>
<h1 id="&#x53EF;&#x4EE5;&#x4E3A;null&#x7684;&#x7C7B;&#x578B;">&#x53EF;&#x4EE5;&#x4E3A;null&#x7684;&#x7C7B;&#x578B;</h1>
<p>TypeScript&#x5177;&#x6709;&#x4E24;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x7C7B;&#x578B;&#xFF0C;<code>null</code>&#x548C;<code>undefined</code>&#xFF0C;&#x5B83;&#x4EEC;&#x5206;&#x522B;&#x5177;&#x6709;&#x503C;null&#x548C;undefined.
&#x6211;&#x4EEC;&#x5728;<a href="Basic%20Types.md">&#x57FA;&#x7840;&#x7C7B;&#x578B;</a>&#x4E00;&#x8282;&#x91CC;&#x5DF2;&#x7ECF;&#x505A;&#x8FC7;&#x7B80;&#x8981;&#x8BF4;&#x660E;&#x3002;
&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x5668;&#x8BA4;&#x4E3A;<code>null</code>&#x4E0E;<code>undefined</code>&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x3002;
<code>null</code>&#x4E0E;<code>undefined</code>&#x662F;&#x6240;&#x6709;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x503C;&#x3002;
&#x8FD9;&#x4E5F;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x4F60;&#x963B;&#x6B62;&#x4E0D;&#x4E86;&#x5C06;&#x5B83;&#x4EEC;&#x8D4B;&#x503C;&#x7ED9;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#xFF0C;&#x5C31;&#x7B97;&#x662F;&#x4F60;&#x60F3;&#x8981;&#x963B;&#x6B62;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E5F;&#x4E0D;&#x884C;&#x3002;
<code>null</code>&#x7684;&#x53D1;&#x660E;&#x8005;&#xFF0C;Tony Hoare&#xFF0C;&#x79F0;&#x5B83;&#x4E3A;<a href="https://en.wikipedia.org/wiki/Null_pointer#History" target="_blank">&#x4EF7;&#x503C;&#x4EBF;&#x4E07;&#x7F8E;&#x91D1;&#x7684;&#x9519;&#x8BEF;</a>&#x3002;</p>
<p><code>--strictNullChecks</code>&#x6807;&#x8BB0;&#x53EF;&#x4EE5;&#x89E3;&#x51B3;&#x6B64;&#x9519;&#x8BEF;&#xFF1A;&#x5F53;&#x4F60;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x5B83;&#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x5730;&#x5305;&#x542B;<code>null</code>&#x6216;<code>undefined</code>&#x3002;
&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x660E;&#x786E;&#x7684;&#x5305;&#x542B;&#x5B83;&#x4EEC;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> s = <span class="hljs-string">&quot;foo&quot;</span>;
s = <span class="hljs-literal">null</span>; <span class="hljs-comment">// &#x9519;&#x8BEF;, &apos;null&apos;&#x4E0D;&#x80FD;&#x8D4B;&#x503C;&#x7ED9;&apos;string&apos;</span>
<span class="hljs-keyword">let</span> sn: <span class="hljs-built_in">string</span> | <span class="hljs-literal">null</span> = <span class="hljs-string">&quot;bar&quot;</span>;
sn = <span class="hljs-literal">null</span>; <span class="hljs-comment">// &#x53EF;&#x4EE5;</span>

sn = <span class="hljs-literal">undefined</span>; <span class="hljs-comment">// error, &apos;undefined&apos;&#x4E0D;&#x80FD;&#x8D4B;&#x503C;&#x7ED9;&apos;string | null&apos;</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x6309;&#x7167;JavaScript&#x7684;&#x8BED;&#x4E49;&#xFF0C;TypeScript&#x4F1A;&#x628A;<code>null</code>&#x548C;<code>undefined</code>&#x533A;&#x522B;&#x5BF9;&#x5F85;&#x3002;
<code>string | null</code>&#xFF0C;<code>string | undefined</code>&#x548C;<code>string | undefined | null</code>&#x662F;&#x4E0D;&#x540C;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<h2 id="&#x53EF;&#x9009;&#x53C2;&#x6570;&#x548C;&#x53EF;&#x9009;&#x5C5E;&#x6027;">&#x53EF;&#x9009;&#x53C2;&#x6570;&#x548C;&#x53EF;&#x9009;&#x5C5E;&#x6027;</h2>
<p>&#x4F7F;&#x7528;&#x4E86;<code>--strictNullChecks</code>&#xFF0C;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x4F1A;&#x88AB;&#x81EA;&#x52A8;&#x5730;&#x52A0;&#x4E0A;<code>| undefined</code>:</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">x: <span class="hljs-built_in">number</span>, y?: <span class="hljs-built_in">number</span></span>) </span>{
    <span class="hljs-keyword">return</span> x + (y || <span class="hljs-number">0</span>);
}
f(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
f(<span class="hljs-number">1</span>);
f(<span class="hljs-number">1</span>, <span class="hljs-literal">undefined</span>);
f(<span class="hljs-number">1</span>, <span class="hljs-literal">null</span>); <span class="hljs-comment">// error, &apos;null&apos; is not assignable to &apos;number | undefined&apos;</span>
</code></pre>
<p>&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x4E5F;&#x4F1A;&#x6709;&#x540C;&#x6837;&#x7684;&#x5904;&#x7406;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> C {
    a: <span class="hljs-built_in">number</span>;
    b?: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">let</span> c = <span class="hljs-keyword">new</span> C();
c.a = <span class="hljs-number">12</span>;
c.a = <span class="hljs-literal">undefined</span>; <span class="hljs-comment">// error, &apos;undefined&apos; is not assignable to &apos;number&apos;</span>
c.b = <span class="hljs-number">13</span>;
c.b = <span class="hljs-literal">undefined</span>; <span class="hljs-comment">// ok</span>
c.b = <span class="hljs-literal">null</span>; <span class="hljs-comment">// error, &apos;null&apos; is not assignable to &apos;number | undefined&apos;</span>
</code></pre>
<h2 id="&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x548C;&#x7C7B;&#x578B;&#x65AD;&#x8A00;">&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x548C;&#x7C7B;&#x578B;&#x65AD;&#x8A00;</h2>
<p>&#x7531;&#x4E8E;&#x53EF;&#x4EE5;&#x4E3A;null&#x7684;&#x7C7B;&#x578B;&#x662F;&#x901A;&#x8FC7;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x5B9E;&#x73B0;&#xFF0C;&#x90A3;&#x4E48;&#x4F60;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x6765;&#x53BB;&#x9664;<code>null</code>&#x3002;
&#x5E78;&#x8FD0;&#x5730;&#x662F;&#x8FD9;&#x4E0E;&#x5728;JavaScript&#x91CC;&#x5199;&#x7684;&#x4EE3;&#x7801;&#x4E00;&#x81F4;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">sn: <span class="hljs-built_in">string</span> | <span class="hljs-literal">null</span></span>): <span class="hljs-title">string</span> </span>{
    <span class="hljs-keyword">if</span> (sn == <span class="hljs-literal">null</span>) {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;default&quot;</span>;
    }
    <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> sn;
    }
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x5F88;&#x660E;&#x663E;&#x5730;&#x53BB;&#x9664;&#x4E86;<code>null</code>&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x77ED;&#x8DEF;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">sn: <span class="hljs-built_in">string</span> | <span class="hljs-literal">null</span></span>): <span class="hljs-title">string</span> </span>{
    <span class="hljs-keyword">return</span> sn || <span class="hljs-string">&quot;default&quot;</span>;
}
</code></pre>
<p>&#x5982;&#x679C;&#x7F16;&#x8BD1;&#x5668;&#x4E0D;&#x80FD;&#x591F;&#x53BB;&#x9664;<code>null</code>&#x6216;<code>undefined</code>&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x65AD;&#x8A00;&#x624B;&#x52A8;&#x53BB;&#x9664;&#x3002;
&#x8BED;&#x6CD5;&#x662F;&#x6DFB;&#x52A0;<code>!</code>&#x540E;&#x7F00;&#xFF1A;<code>identifier!</code>&#x4ECE;<code>identifier</code>&#x7684;&#x7C7B;&#x578B;&#x91CC;&#x53BB;&#x9664;&#x4E86;<code>null</code>&#x548C;<code>undefined</code>&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">broken</span>(<span class="hljs-params">name: <span class="hljs-built_in">string</span> | <span class="hljs-literal">null</span></span>): <span class="hljs-title">string</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">postfix</span>(<span class="hljs-params">epithet: <span class="hljs-built_in">string</span></span>) </span>{
    <span class="hljs-keyword">return</span> name.charAt(<span class="hljs-number">0</span>) + <span class="hljs-string">&apos;.  the &apos;</span> + epithet; <span class="hljs-comment">// error, &apos;name&apos; is possibly null</span>
  }
  name = name || <span class="hljs-string">&quot;Bob&quot;</span>;
  <span class="hljs-keyword">return</span> postfix(<span class="hljs-string">&quot;great&quot;</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fixed</span>(<span class="hljs-params">name: <span class="hljs-built_in">string</span> | <span class="hljs-literal">null</span></span>): <span class="hljs-title">string</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">postfix</span>(<span class="hljs-params">epithet: <span class="hljs-built_in">string</span></span>) </span>{
    <span class="hljs-keyword">return</span> name!.charAt(<span class="hljs-number">0</span>) + <span class="hljs-string">&apos;.  the &apos;</span> + epithet; <span class="hljs-comment">// ok</span>
  }
  name = name || <span class="hljs-string">&quot;Bob&quot;</span>;
  <span class="hljs-keyword">return</span> postfix(<span class="hljs-string">&quot;great&quot;</span>);
}
</code></pre>
<p>&#x672C;&#x4F8B;&#x4F7F;&#x7528;&#x4E86;&#x5D4C;&#x5957;&#x51FD;&#x6570;&#xFF0C;&#x56E0;&#x4E3A;&#x7F16;&#x8BD1;&#x5668;&#x65E0;&#x6CD5;&#x53BB;&#x9664;&#x5D4C;&#x5957;&#x51FD;&#x6570;&#x7684;null&#xFF08;&#x9664;&#x975E;&#x662F;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#xFF09;&#x3002;
&#x56E0;&#x4E3A;&#x5B83;&#x65E0;&#x6CD5;&#x8DDF;&#x8E2A;&#x6240;&#x6709;&#x5BF9;&#x5D4C;&#x5957;&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#xFF0C;&#x5C24;&#x5176;&#x662F;&#x4F60;&#x5C06;&#x5185;&#x5C42;&#x51FD;&#x6570;&#x505A;&#x4E3A;&#x5916;&#x5C42;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;
&#x5982;&#x679C;&#x65E0;&#x6CD5;&#x77E5;&#x9053;&#x51FD;&#x6570;&#x5728;&#x54EA;&#x91CC;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x5C31;&#x65E0;&#x6CD5;&#x77E5;&#x9053;&#x8C03;&#x7528;&#x65F6;<code>name</code>&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<h1 id="&#x7C7B;&#x578B;&#x522B;&#x540D;">&#x7C7B;&#x578B;&#x522B;&#x540D;</h1>
<p>&#x7C7B;&#x578B;&#x522B;&#x540D;&#x4F1A;&#x7ED9;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x8D77;&#x4E2A;&#x65B0;&#x540D;&#x5B57;&#x3002;
&#x7C7B;&#x578B;&#x522B;&#x540D;&#x6709;&#x65F6;&#x548C;&#x63A5;&#x53E3;&#x5F88;&#x50CF;&#xFF0C;&#x4F46;&#x662F;&#x53EF;&#x4EE5;&#x4F5C;&#x7528;&#x4E8E;&#x539F;&#x59CB;&#x503C;&#xFF0C;&#x8054;&#x5408;&#x7C7B;&#x578B;&#xFF0C;&#x5143;&#x7EC4;&#x4EE5;&#x53CA;&#x5176;&#x5B83;&#x4EFB;&#x4F55;&#x4F60;&#x9700;&#x8981;&#x624B;&#x5199;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Name = <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">type</span> NameResolver = () =&gt; <span class="hljs-built_in">string</span>;
<span class="hljs-keyword">type</span> NameOrResolver = Name | NameResolver;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getName</span>(<span class="hljs-params">n: NameOrResolver</span>): <span class="hljs-title">Name</span> </span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> n === <span class="hljs-string">&apos;string&apos;</span>) {
        <span class="hljs-keyword">return</span> n;
    }
    <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> n();
    }
}
</code></pre>
<p>&#x8D77;&#x522B;&#x540D;&#x4E0D;&#x4F1A;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;&#x578B; - &#x5B83;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;<em>&#x540D;&#x5B57;</em>&#x6765;&#x5F15;&#x7528;&#x90A3;&#x4E2A;&#x7C7B;&#x578B;&#x3002;
&#x7ED9;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x8D77;&#x522B;&#x540D;&#x901A;&#x5E38;&#x6CA1;&#x4EC0;&#x4E48;&#x7528;&#xFF0C;&#x5C3D;&#x7BA1;&#x53EF;&#x4EE5;&#x505A;&#x4E3A;&#x6587;&#x6863;&#x7684;&#x4E00;&#x79CD;&#x5F62;&#x5F0F;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x540C;&#x63A5;&#x53E3;&#x4E00;&#x6837;&#xFF0C;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x6CDB;&#x578B; - &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x5E76;&#x4E14;&#x5728;&#x522B;&#x540D;&#x58F0;&#x660E;&#x7684;&#x53F3;&#x4FA7;&#x4F20;&#x5165;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Container&lt;T&gt; = { value: T };
</code></pre>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x6765;&#x5728;&#x5C5E;&#x6027;&#x91CC;&#x5F15;&#x7528;&#x81EA;&#x5DF1;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Tree&lt;T&gt; = {
    value: T;
    left: Tree&lt;T&gt;;
    right: Tree&lt;T&gt;;
}
</code></pre>
<p>&#x4E0E;&#x4EA4;&#x53C9;&#x7C7B;&#x578B;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x51FA;&#x4E00;&#x4E9B;&#x5341;&#x5206;&#x7A00;&#x5947;&#x53E4;&#x602A;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> LinkedList&lt;T&gt; = T &amp; { next: LinkedList&lt;T&gt; };

<span class="hljs-keyword">interface</span> Person {
    name: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">var</span> people: LinkedList&lt;Person&gt;;
<span class="hljs-keyword">var</span> s = people.name;
<span class="hljs-keyword">var</span> s = people.next.name;
<span class="hljs-keyword">var</span> s = people.next.next.name;
<span class="hljs-keyword">var</span> s = people.next.next.next.name;
</code></pre>
<p>&#x7136;&#x800C;&#xFF0C;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x4E0D;&#x80FD;&#x51FA;&#x73B0;&#x5728;&#x58F0;&#x660E;&#x53F3;&#x4FA7;&#x7684;&#x4EFB;&#x4F55;&#x5730;&#x65B9;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Yikes = <span class="hljs-built_in">Array</span>&lt;Yikes&gt;; <span class="hljs-comment">// error</span>
</code></pre>
<h2 id="&#x63A5;&#x53E3;-vs-&#x7C7B;&#x578B;&#x522B;&#x540D;">&#x63A5;&#x53E3; vs. &#x7C7B;&#x578B;&#x522B;&#x540D;</h2>
<p>&#x50CF;&#x6211;&#x4EEC;&#x63D0;&#x5230;&#x7684;&#xFF0C;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x53EF;&#x4EE5;&#x50CF;&#x63A5;&#x53E3;&#x4E00;&#x6837;&#xFF1B;&#x7136;&#x800C;&#xFF0C;&#x4ECD;&#x6709;&#x4E00;&#x4E9B;&#x7EC6;&#x5FAE;&#x5DEE;&#x522B;&#x3002;</p>
<p>&#x5176;&#x4E00;&#xFF0C;&#x63A5;&#x53E3;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x5176;&#x5B83;&#x4EFB;&#x4F55;&#x5730;&#x65B9;&#x4F7F;&#x7528;&#x3002;
&#x7C7B;&#x578B;&#x522B;&#x540D;&#x5E76;&#x4E0D;&#x521B;&#x5EFA;&#x65B0;&#x540D;&#x5B57;&#x2014;&#x6BD4;&#x5982;&#xFF0C;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x5C31;&#x4E0D;&#x4F1A;&#x4F7F;&#x7528;&#x522B;&#x540D;&#x3002;
&#x5728;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x91CC;&#xFF0C;&#x5728;&#x7F16;&#x8BD1;&#x5668;&#x4E2D;&#x5C06;&#x9F20;&#x6807;&#x60AC;&#x505C;&#x5728;<code>interfaced</code>&#x4E0A;&#xFF0C;&#x663E;&#x793A;&#x5B83;&#x8FD4;&#x56DE;&#x7684;&#x662F;<code>Interface</code>&#xFF0C;&#x4F46;&#x60AC;&#x505C;&#x5728;<code>aliased</code>&#x4E0A;&#x65F6;&#xFF0C;&#x663E;&#x793A;&#x7684;&#x5374;&#x662F;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Alias = { num: <span class="hljs-built_in">number</span> }
<span class="hljs-keyword">interface</span> Interface {
    num: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">aliased</span>(<span class="hljs-params">arg: Alias</span>): <span class="hljs-title">Alias</span></span>;
<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">interfaced</span>(<span class="hljs-params">arg: Interface</span>): <span class="hljs-title">Interface</span></span>;
</code></pre>
<p>&#x53E6;&#x4E00;&#x4E2A;&#x91CD;&#x8981;&#x533A;&#x522B;&#x662F;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x4E0D;&#x80FD;&#x88AB;<code>extends</code>&#x548C;<code>implements</code>&#xFF08;&#x81EA;&#x5DF1;&#x4E5F;&#x4E0D;&#x80FD;<code>extends</code>&#x548C;<code>implements</code>&#x5176;&#x5B83;&#x7C7B;&#x578B;&#xFF09;&#x3002;
&#x56E0;&#x4E3A;<a href="https://en.wikipedia.org/wiki/Open/closed_principle" target="_blank">&#x8F6F;&#x4EF6;&#x4E2D;&#x7684;&#x5BF9;&#x8C61;&#x5E94;&#x8BE5;&#x5BF9;&#x4E8E;&#x6269;&#x5C55;&#x662F;&#x5F00;&#x653E;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5BF9;&#x4E8E;&#x4FEE;&#x6539;&#x662F;&#x5C01;&#x95ED;&#x7684;</a>&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x5C3D;&#x91CF;&#x53BB;&#x4F7F;&#x7528;&#x63A5;&#x53E3;&#x4EE3;&#x66FF;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x3002;</p>
<p>&#x53E6;&#x4E00;&#x65B9;&#x9762;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x65E0;&#x6CD5;&#x901A;&#x8FC7;&#x63A5;&#x53E3;&#x6765;&#x63CF;&#x8FF0;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x5E76;&#x4E14;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x6216;&#x5143;&#x7EC4;&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x65F6;&#x901A;&#x5E38;&#x4F1A;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x3002;</p>
<h1 id="&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;">&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;</h1>
<p>&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x5141;&#x8BB8;&#x4F60;&#x6307;&#x5B9A;&#x5B57;&#x7B26;&#x4E32;&#x5FC5;&#x987B;&#x7684;&#x56FA;&#x5B9A;&#x503C;&#x3002;
&#x5728;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x4E0E;&#x8054;&#x5408;&#x7C7B;&#x578B;&#xFF0C;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x548C;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x5F88;&#x597D;&#x7684;&#x914D;&#x5408;&#x3002;
&#x901A;&#x8FC7;&#x7ED3;&#x5408;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x7279;&#x6027;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x7C7B;&#x4F3C;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Easing = <span class="hljs-string">&quot;ease-in&quot;</span> | <span class="hljs-string">&quot;ease-out&quot;</span> | <span class="hljs-string">&quot;ease-in-out&quot;</span>;
<span class="hljs-keyword">class</span> UIElement {
    animate(dx: <span class="hljs-built_in">number</span>, dy: <span class="hljs-built_in">number</span>, easing: Easing) {
        <span class="hljs-keyword">if</span> (easing === <span class="hljs-string">&quot;ease-in&quot;</span>) {
            <span class="hljs-comment">// ...</span>
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (easing === <span class="hljs-string">&quot;ease-out&quot;</span>) {
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (easing === <span class="hljs-string">&quot;ease-in-out&quot;</span>) {
        }
        <span class="hljs-keyword">else</span> {
            <span class="hljs-comment">// error! should not pass null or undefined.</span>
        }
    }
}

<span class="hljs-keyword">let</span> button = <span class="hljs-keyword">new</span> UIElement();
button.animate(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-string">&quot;ease-in&quot;</span>);
button.animate(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-string">&quot;uneasy&quot;</span>); <span class="hljs-comment">// error: &quot;uneasy&quot; is not allowed here</span>
</code></pre>
<p>&#x4F60;&#x53EA;&#x80FD;&#x4ECE;&#x4E09;&#x79CD;&#x5141;&#x8BB8;&#x7684;&#x5B57;&#x7B26;&#x4E2D;&#x9009;&#x62E9;&#x5176;&#x4E00;&#x6765;&#x505A;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#xFF0C;&#x4F20;&#x5165;&#x5176;&#x5B83;&#x503C;&#x5219;&#x4F1A;&#x4EA7;&#x751F;&#x9519;&#x8BEF;&#x3002;</p>
<pre><code class="lang-text">Argument of type &apos;&quot;uneasy&quot;&apos; is not assignable to parameter of type &apos;&quot;ease-in&quot; | &quot;ease-out&quot; | &quot;ease-in-out&quot;&apos;
</code></pre>
<p>&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x8FD8;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x533A;&#x5206;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#xFF1A;</p>
<pre><code class="lang-ts">function createElement(tagName: &quot;img&quot;): HTMLImageElement;
function createElement(tagName: &quot;input&quot;): HTMLInputElement;
// ... more overloads ...
function createElement(tagName: string): Element {
    // ... code goes here ...
}
</code></pre>
<h1 id="&#x6570;&#x5B57;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;">&#x6570;&#x5B57;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;</h1>
<p>TypeScript&#x8FD8;&#x5177;&#x6709;&#x6570;&#x5B57;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">rollDie</span>(<span class="hljs-params"></span>): 1 | 2 | 3 | 4 | 5 | 6 </span>{
    <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x5F88;&#x5C11;&#x76F4;&#x63A5;&#x8FD9;&#x6837;&#x4F7F;&#x7528;&#xFF0C;&#x4F46;&#x5B83;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x5728;&#x7F29;&#x5C0F;&#x8303;&#x56F4;&#x8C03;&#x8BD5;bug&#x7684;&#x65F6;&#x5019;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">x: <span class="hljs-built_in">number</span></span>) </span>{
    <span class="hljs-keyword">if</span> (x !== <span class="hljs-number">1</span> || x !== <span class="hljs-number">2</span>) {
        <span class="hljs-comment">//         ~~~~~~~</span>
        <span class="hljs-comment">// Operator &apos;!==&apos; cannot be applied to types &apos;1&apos; and &apos;2&apos;.</span>
    }
}
</code></pre>
<p>&#x6362;&#x53E5;&#x8BDD;&#x8BF4;&#xFF0C;&#x5F53;<code>x</code>&#x4E0E;<code>2</code>&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5B83;&#x7684;&#x503C;&#x5FC5;&#x987B;&#x4E3A;<code>1</code>&#xFF0C;&#x8FD9;&#x5C31;&#x610F;&#x5473;&#x7740;&#x4E0A;&#x9762;&#x7684;&#x6BD4;&#x8F83;&#x68C0;&#x67E5;&#x662F;&#x975E;&#x6CD5;&#x7684;&#x3002;</p>
<h1 id="&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7C7B;&#x578B;">&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7C7B;&#x578B;</h1>
<p>&#x5982;&#x6211;&#x4EEC;&#x5728;<a href="Enums.html#union-enums-and-enum-member-types">&#x679A;&#x4E3E;</a>&#x4E00;&#x8282;&#x91CC;&#x63D0;&#x5230;&#x7684;&#xFF0C;&#x5F53;&#x6BCF;&#x4E2A;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x90FD;&#x662F;&#x7528;&#x5B57;&#x9762;&#x91CF;&#x521D;&#x59CB;&#x5316;&#x7684;&#x65F6;&#x5019;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x662F;&#x5177;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x3002;</p>
<p>&#x5728;&#x6211;&#x4EEC;&#x8C08;&#x53CA;&#x201C;&#x5355;&#x4F8B;&#x7C7B;&#x578B;&#x201D;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x591A;&#x6570;&#x662F;&#x6307;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7C7B;&#x578B;&#x548C;&#x6570;&#x5B57;/&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#xFF0C;&#x5C3D;&#x7BA1;&#x5927;&#x591A;&#x6570;&#x7528;&#x6237;&#x4F1A;&#x4E92;&#x6362;&#x4F7F;&#x7528;&#x201C;&#x5355;&#x4F8B;&#x7C7B;&#x578B;&#x201D;&#x548C;&#x201C;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x201D;&#x3002;</p>
<h1 id="&#x53EF;&#x8FA8;&#x8BC6;&#x8054;&#x5408;&#xFF08;discriminated-unions&#xFF09;">&#x53EF;&#x8FA8;&#x8BC6;&#x8054;&#x5408;&#xFF08;Discriminated Unions&#xFF09;</h1>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5408;&#x5E76;&#x5355;&#x4F8B;&#x7C7B;&#x578B;&#xFF0C;&#x8054;&#x5408;&#x7C7B;&#x578B;&#xFF0C;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x548C;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x53EB;&#x505A;<em>&#x53EF;&#x8FA8;&#x8BC6;&#x8054;&#x5408;</em>&#x7684;&#x9AD8;&#x7EA7;&#x6A21;&#x5F0F;&#xFF0C;&#x5B83;&#x4E5F;&#x79F0;&#x505A;<em>&#x6807;&#x7B7E;&#x8054;&#x5408;</em>&#x6216;<em>&#x4EE3;&#x6570;&#x6570;&#x636E;&#x7C7B;&#x578B;</em>&#x3002;
&#x53EF;&#x8FA8;&#x8BC6;&#x8054;&#x5408;&#x5728;&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;&#x5F88;&#x6709;&#x7528;&#x5904;&#x3002;
&#x4E00;&#x4E9B;&#x8BED;&#x8A00;&#x4F1A;&#x81EA;&#x52A8;&#x5730;&#x4E3A;&#x4F60;&#x8FA8;&#x8BC6;&#x8054;&#x5408;&#xFF1B;&#x800C;TypeScript&#x5219;&#x57FA;&#x4E8E;&#x5DF2;&#x6709;&#x7684;JavaScript&#x6A21;&#x5F0F;&#x3002;
&#x5B83;&#x5177;&#x6709;3&#x4E2A;&#x8981;&#x7D20;&#xFF1A;</p>
<ol>
<li>&#x5177;&#x6709;&#x666E;&#x901A;&#x7684;&#x5355;&#x4F8B;&#x7C7B;&#x578B;&#x5C5E;&#x6027;&#x2014;<em>&#x53EF;&#x8FA8;&#x8BC6;&#x7684;&#x7279;&#x5F81;</em>&#x3002;</li>
<li>&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x5305;&#x542B;&#x4E86;&#x90A3;&#x4E9B;&#x7C7B;&#x578B;&#x7684;&#x8054;&#x5408;&#x2014;<em>&#x8054;&#x5408;</em>&#x3002;</li>
<li>&#x6B64;&#x5C5E;&#x6027;&#x4E0A;&#x7684;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x3002;</li>
</ol>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Square {
    kind: <span class="hljs-string">&quot;square&quot;</span>;
    size: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">interface</span> Rectangle {
    kind: <span class="hljs-string">&quot;rectangle&quot;</span>;
    width: <span class="hljs-built_in">number</span>;
    height: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">interface</span> Circle {
    kind: <span class="hljs-string">&quot;circle&quot;</span>;
    radius: <span class="hljs-built_in">number</span>;
}
</code></pre>
<p>&#x9996;&#x5148;&#x6211;&#x4EEC;&#x58F0;&#x660E;&#x4E86;&#x5C06;&#x8981;&#x8054;&#x5408;&#x7684;&#x63A5;&#x53E3;&#x3002;
&#x6BCF;&#x4E2A;&#x63A5;&#x53E3;&#x90FD;&#x6709;<code>kind</code>&#x5C5E;&#x6027;&#x4F46;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x3002;
<code>kind</code>&#x5C5E;&#x6027;&#x79F0;&#x505A;<em>&#x53EF;&#x8FA8;&#x8BC6;&#x7684;&#x7279;&#x5F81;</em>&#x6216;<em>&#x6807;&#x7B7E;</em>&#x3002;
&#x5176;&#x5B83;&#x7684;&#x5C5E;&#x6027;&#x5219;&#x7279;&#x5B9A;&#x4E8E;&#x5404;&#x4E2A;&#x63A5;&#x53E3;&#x3002;
&#x6CE8;&#x610F;&#xFF0C;&#x76EE;&#x524D;&#x5404;&#x4E2A;&#x63A5;&#x53E3;&#x95F4;&#x662F;&#x6CA1;&#x6709;&#x8054;&#x7CFB;&#x7684;&#x3002;
&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x628A;&#x5B83;&#x4EEC;&#x8054;&#x5408;&#x5230;&#x4E00;&#x8D77;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Shape = Square | Rectangle | Circle;
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x53EF;&#x8FA8;&#x8BC6;&#x8054;&#x5408;:</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">area</span>(<span class="hljs-params">s: Shape</span>) </span>{
    <span class="hljs-keyword">switch</span> (s.kind) {
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;square&quot;</span>: <span class="hljs-keyword">return</span> s.size * s.size;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;rectangle&quot;</span>: <span class="hljs-keyword">return</span> s.height * s.width;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;circle&quot;</span>: <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.PI * s.radius ** <span class="hljs-number">2</span>;
    }
}
</code></pre>
<h2 id="&#x5B8C;&#x6574;&#x6027;&#x68C0;&#x67E5;">&#x5B8C;&#x6574;&#x6027;&#x68C0;&#x67E5;</h2>
<p>&#x5F53;&#x6CA1;&#x6709;&#x6DB5;&#x76D6;&#x6240;&#x6709;&#x53EF;&#x8FA8;&#x8BC6;&#x8054;&#x5408;&#x7684;&#x53D8;&#x5316;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x60F3;&#x8BA9;&#x7F16;&#x8BD1;&#x5668;&#x53EF;&#x4EE5;&#x901A;&#x77E5;&#x6211;&#x4EEC;&#x3002;
&#x6BD4;&#x5982;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E86;<code>Triangle</code>&#x5230;<code>Shape</code>&#xFF0C;&#x6211;&#x4EEC;&#x540C;&#x65F6;&#x8FD8;&#x9700;&#x8981;&#x66F4;&#x65B0;<code>area</code>:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Shape = Square | Rectangle | Circle | Triangle;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">area</span>(<span class="hljs-params">s: Shape</span>) </span>{
    <span class="hljs-keyword">switch</span> (s.kind) {
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;square&quot;</span>: <span class="hljs-keyword">return</span> s.size * s.size;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;rectangle&quot;</span>: <span class="hljs-keyword">return</span> s.height * s.width;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;circle&quot;</span>: <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.PI * s.radius ** <span class="hljs-number">2</span>;
    }
    <span class="hljs-comment">// should error here - we didn&apos;t handle case &quot;triangle&quot;</span>
}
</code></pre>
<p>&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x3002;
&#x9996;&#x5148;&#x662F;&#x542F;&#x7528;<code>--strictNullChecks</code>&#x5E76;&#x4E14;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">area</span>(<span class="hljs-params">s: Shape</span>): <span class="hljs-title">number</span> </span>{ <span class="hljs-comment">// error: returns number | undefined</span>
    <span class="hljs-keyword">switch</span> (s.kind) {
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;square&quot;</span>: <span class="hljs-keyword">return</span> s.size * s.size;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;rectangle&quot;</span>: <span class="hljs-keyword">return</span> s.height * s.width;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;circle&quot;</span>: <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.PI * s.radius ** <span class="hljs-number">2</span>;
    }
}
</code></pre>
<p>&#x56E0;&#x4E3A;<code>switch</code>&#x6CA1;&#x6709;&#x5305;&#x6DB5;&#x6240;&#x6709;&#x60C5;&#x51B5;&#xFF0C;&#x6240;&#x4EE5;TypeScript&#x8BA4;&#x4E3A;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x6709;&#x65F6;&#x5019;&#x4F1A;&#x8FD4;&#x56DE;<code>undefined</code>&#x3002;
&#x5982;&#x679C;&#x4F60;&#x660E;&#x786E;&#x5730;&#x6307;&#x5B9A;&#x4E86;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x4E3A;<code>number</code>&#xFF0C;&#x90A3;&#x4E48;&#x4F60;&#x4F1A;&#x770B;&#x5230;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x56E0;&#x4E3A;&#x5B9E;&#x9645;&#x4E0A;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x4E3A;<code>number | undefined</code>&#x3002;
&#x7136;&#x800C;&#xFF0C;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x5B58;&#x5728;&#x4E9B;&#x5FAE;&#x5999;&#x4E4B;&#x5904;&#x4E14;<code>--strictNullChecks</code>&#x5BF9;&#x65E7;&#x4EE3;&#x7801;&#x652F;&#x6301;&#x4E0D;&#x597D;&#x3002;</p>
<p>&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x6CD5;&#x4F7F;&#x7528;<code>never</code>&#x7C7B;&#x578B;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x7528;&#x5B83;&#x6765;&#x8FDB;&#x884C;&#x5B8C;&#x6574;&#x6027;&#x68C0;&#x67E5;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">assertNever</span>(<span class="hljs-params">x: never</span>): <span class="hljs-title">never</span> </span>{
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">&quot;Unexpected object: &quot;</span> + x);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">area</span>(<span class="hljs-params">s: Shape</span>) </span>{
    <span class="hljs-keyword">switch</span> (s.kind) {
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;square&quot;</span>: <span class="hljs-keyword">return</span> s.size * s.size;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;rectangle&quot;</span>: <span class="hljs-keyword">return</span> s.height * s.width;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;circle&quot;</span>: <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.PI * s.radius ** <span class="hljs-number">2</span>;
        <span class="hljs-keyword">default</span>: <span class="hljs-keyword">return</span> assertNever(s); <span class="hljs-comment">// error here if there are missing cases</span>
    }
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#xFF0C;<code>assertNever</code>&#x68C0;&#x67E5;<code>s</code>&#x662F;&#x5426;&#x4E3A;<code>never</code>&#x7C7B;&#x578B;&#x2014;&#x5373;&#x4E3A;&#x9664;&#x53BB;&#x6240;&#x6709;&#x53EF;&#x80FD;&#x60C5;&#x51B5;&#x540E;&#x5269;&#x4E0B;&#x7684;&#x7C7B;&#x578B;&#x3002;
&#x5982;&#x679C;&#x4F60;&#x5FD8;&#x8BB0;&#x4E86;&#x67D0;&#x4E2A;case&#xFF0C;&#x90A3;&#x4E48;<code>s</code>&#x5C06;&#x5177;&#x6709;&#x4E00;&#x4E2A;&#x771F;&#x5B9E;&#x7684;&#x7C7B;&#x578B;&#x5E76;&#x4E14;&#x4F60;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x3002;
&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x9700;&#x8981;&#x4F60;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x989D;&#x5916;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x4F60;&#x5FD8;&#x8BB0;&#x67D0;&#x4E2A;case&#x7684;&#x65F6;&#x5019;&#x4E5F;&#x66F4;&#x52A0;&#x660E;&#x663E;&#x3002;</p>
<h1 id="&#x591A;&#x6001;&#x7684;this&#x7C7B;&#x578B;">&#x591A;&#x6001;&#x7684;<code>this</code>&#x7C7B;&#x578B;</h1>
<p>&#x591A;&#x6001;&#x7684;<code>this</code>&#x7C7B;&#x578B;&#x8868;&#x793A;&#x7684;&#x662F;&#x67D0;&#x4E2A;&#x5305;&#x542B;&#x7C7B;&#x6216;&#x63A5;&#x53E3;&#x7684;<em>&#x5B50;&#x7C7B;&#x578B;</em>&#x3002;
&#x8FD9;&#x88AB;&#x79F0;&#x505A;<em>F</em>-bounded&#x591A;&#x6001;&#x6027;&#x3002;
&#x5B83;&#x80FD;&#x5F88;&#x5BB9;&#x6613;&#x7684;&#x8868;&#x73B0;&#x8FDE;&#x8D2F;&#x63A5;&#x53E3;&#x95F4;&#x7684;&#x7EE7;&#x627F;&#xFF0C;&#x6BD4;&#x5982;&#x3002;
&#x5728;&#x8BA1;&#x7B97;&#x5668;&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;&#x5728;&#x6BCF;&#x4E2A;&#x64CD;&#x4F5C;&#x4E4B;&#x540E;&#x90FD;&#x8FD4;&#x56DE;<code>this</code>&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> BasicCalculator {
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">constructor</span>(protected value: number = 0) { }
    <span class="hljs-keyword">public</span> currentValue(): <span class="hljs-built_in">number</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.value;
    }
    <span class="hljs-keyword">public</span> add(operand: <span class="hljs-built_in">number</span>): <span class="hljs-keyword">this</span> {
        <span class="hljs-keyword">this</span>.value += operand;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
    }
    <span class="hljs-keyword">public</span> multiply(operand: <span class="hljs-built_in">number</span>): <span class="hljs-keyword">this</span> {
        <span class="hljs-keyword">this</span>.value *= operand;
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
    }
    <span class="hljs-comment">// ... other operations go here ...</span>
}

<span class="hljs-keyword">let</span> v = <span class="hljs-keyword">new</span> BasicCalculator(<span class="hljs-number">2</span>)
            .multiply(<span class="hljs-number">5</span>)
            .add(<span class="hljs-number">1</span>)
            .currentValue();
</code></pre>
<p>&#x7531;&#x4E8E;&#x8FD9;&#x4E2A;&#x7C7B;&#x4F7F;&#x7528;&#x4E86;<code>this</code>&#x7C7B;&#x578B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x7EE7;&#x627F;&#x5B83;&#xFF0C;&#x65B0;&#x7684;&#x7C7B;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x505A;&#x4EFB;&#x4F55;&#x7684;&#x6539;&#x53D8;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> ScientificCalculator extends BasicCalculator {
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">constructor</span>(value = 0) {
        <span class="hljs-keyword">super</span>(value);
    }
    <span class="hljs-keyword">public</span> sin() {
        <span class="hljs-keyword">this</span>.value = <span class="hljs-built_in">Math</span>.sin(<span class="hljs-keyword">this</span>.value);
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
    }
    <span class="hljs-comment">// ... other operations go here ...</span>
}

<span class="hljs-keyword">let</span> v = <span class="hljs-keyword">new</span> ScientificCalculator(<span class="hljs-number">2</span>)
        .multiply(<span class="hljs-number">5</span>)
        .sin()
        .add(<span class="hljs-number">1</span>)
        .currentValue();
</code></pre>
<p>&#x5982;&#x679C;&#x6CA1;&#x6709;<code>this</code>&#x7C7B;&#x578B;&#xFF0C;<code>ScientificCalculator</code>&#x5C31;&#x4E0D;&#x80FD;&#x591F;&#x5728;&#x7EE7;&#x627F;<code>BasicCalculator</code>&#x7684;&#x540C;&#x65F6;&#x8FD8;&#x4FDD;&#x6301;&#x63A5;&#x53E3;&#x7684;&#x8FDE;&#x8D2F;&#x6027;&#x3002;
<code>multiply</code>&#x5C06;&#x4F1A;&#x8FD4;&#x56DE;<code>BasicCalculator</code>&#xFF0C;&#x5B83;&#x5E76;&#x6CA1;&#x6709;<code>sin</code>&#x65B9;&#x6CD5;&#x3002;
&#x7136;&#x800C;&#xFF0C;&#x4F7F;&#x7528;<code>this</code>&#x7C7B;&#x578B;&#xFF0C;<code>multiply</code>&#x4F1A;&#x8FD4;&#x56DE;<code>this</code>&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x5C31;&#x662F;<code>ScientificCalculator</code>&#x3002;</p>
<h1 id="&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#xFF08;index-types&#xFF09;">&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#xFF08;Index types&#xFF09;</h1>
<p>&#x4F7F;&#x7528;&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x5C31;&#x80FD;&#x591F;&#x68C0;&#x67E5;&#x4F7F;&#x7528;&#x4E86;&#x52A8;&#x6001;&#x5C5E;&#x6027;&#x540D;&#x7684;&#x4EE3;&#x7801;&#x3002;
&#x4F8B;&#x5982;&#xFF0C;&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x7684;JavaScript&#x6A21;&#x5F0F;&#x662F;&#x4ECE;&#x5BF9;&#x8C61;&#x4E2D;&#x9009;&#x53D6;&#x5C5E;&#x6027;&#x7684;&#x5B50;&#x96C6;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pluck</span>(<span class="hljs-params">o, names</span>) </span>{
    <span class="hljs-keyword">return</span> names.map(n =&gt; o[n]);
}
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;&#x5982;&#x4F55;&#x5728;TypeScript&#x91CC;&#x4F7F;&#x7528;&#x6B64;&#x51FD;&#x6570;&#xFF0C;&#x901A;&#x8FC7;<strong>&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#x67E5;&#x8BE2;</strong>&#x548C;<strong>&#x7D22;&#x5F15;&#x8BBF;&#x95EE;</strong>&#x64CD;&#x4F5C;&#x7B26;&#xFF1A;</p>
<pre><code class="lang-ts">function pluck&lt;T, K extends keyof T&gt;(o: T, names: K[]): T[K][] {
  return names.map(n =&gt; o[n]);
}

interface Person {
    name: string;
    age: number;
}
let person: Person = {
    name: &apos;Jarid&apos;,
    age: 35
};
let strings: string[] = pluck(person, [&apos;name&apos;]); // ok, string[]
</code></pre>
<p>&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x68C0;&#x67E5;<code>name</code>&#x662F;&#x5426;&#x771F;&#x7684;&#x662F;<code>Person</code>&#x7684;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x3002;
&#x672C;&#x4F8B;&#x8FD8;&#x5F15;&#x5165;&#x4E86;&#x51E0;&#x4E2A;&#x65B0;&#x7684;&#x7C7B;&#x578B;&#x64CD;&#x4F5C;&#x7B26;&#x3002;
&#x9996;&#x5148;&#x662F;<code>keyof T</code>&#xFF0C;<strong>&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#x67E5;&#x8BE2;&#x64CD;&#x4F5C;&#x7B26;</strong>&#x3002;
&#x5BF9;&#x4E8E;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;<code>T</code>&#xFF0C;<code>keyof T</code>&#x7684;&#x7ED3;&#x679C;&#x4E3A;<code>T</code>&#x4E0A;&#x5DF2;&#x77E5;&#x7684;&#x516C;&#x5171;&#x5C5E;&#x6027;&#x540D;&#x7684;&#x8054;&#x5408;&#x3002;
&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> personProps: keyof Person; <span class="hljs-comment">// &apos;name&apos; | &apos;age&apos;</span>
</code></pre>
<p><code>keyof Person</code>&#x662F;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x4E0E;<code>&apos;name&apos; | &apos;age&apos;</code>&#x4E92;&#x76F8;&#x66FF;&#x6362;&#x7684;&#x3002;
&#x4E0D;&#x540C;&#x7684;&#x662F;&#x5982;&#x679C;&#x4F60;&#x6DFB;&#x52A0;&#x4E86;&#x5176;&#x5B83;&#x7684;&#x5C5E;&#x6027;&#x5230;<code>Person</code>&#xFF0C;&#x4F8B;&#x5982;<code>address: string</code>&#xFF0C;&#x90A3;&#x4E48;<code>keyof Person</code>&#x4F1A;&#x81EA;&#x52A8;&#x53D8;&#x4E3A;<code>&apos;name&apos; | &apos;age&apos; | &apos;address&apos;</code>&#x3002;
&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x50CF;<code>pluck</code>&#x51FD;&#x6570;&#x8FD9;&#x7C7B;&#x4E0A;&#x4E0B;&#x6587;&#x91CC;&#x4F7F;&#x7528;<code>keyof</code>&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x4F60;&#x5E76;&#x4E0D;&#x6E05;&#x695A;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x3002;
&#x4F46;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x68C0;&#x67E5;&#x4F60;&#x662F;&#x5426;&#x4F20;&#x5165;&#x4E86;&#x6B63;&#x786E;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x7ED9;<code>pluck</code>&#xFF1A;</p>
<pre><code class="lang-ts">pluck(person, [<span class="hljs-string">&apos;age&apos;</span>, <span class="hljs-string">&apos;unknown&apos;</span>]); <span class="hljs-comment">// error, &apos;unknown&apos; is not in &apos;name&apos; | &apos;age&apos;</span>
</code></pre>
<p>&#x7B2C;&#x4E8C;&#x4E2A;&#x64CD;&#x4F5C;&#x7B26;&#x662F;<code>T[K]</code>&#xFF0C;<strong>&#x7D22;&#x5F15;&#x8BBF;&#x95EE;&#x64CD;&#x4F5C;&#x7B26;</strong>&#x3002;
&#x5728;&#x8FD9;&#x91CC;&#xFF0C;&#x7C7B;&#x578B;&#x8BED;&#x6CD5;&#x53CD;&#x6620;&#x4E86;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;&#x3002;
&#x8FD9;&#x610F;&#x5473;&#x7740;<code>person[&apos;name&apos;]</code>&#x5177;&#x6709;&#x7C7B;&#x578B;<code>Person[&apos;name&apos;]</code> &#x2014; &#x5728;&#x6211;&#x4EEC;&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#x5219;&#x4E3A;<code>string</code>&#x7C7B;&#x578B;&#x3002;
&#x7136;&#x800C;&#xFF0C;&#x5C31;&#x50CF;&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#x67E5;&#x8BE2;&#x4E00;&#x6837;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x666E;&#x901A;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x91CC;&#x4F7F;&#x7528;<code>T[K]</code>&#xFF0C;&#x8FD9;&#x6B63;&#x662F;&#x5B83;&#x7684;&#x5F3A;&#x5927;&#x6240;&#x5728;&#x3002;
&#x4F60;&#x53EA;&#x8981;&#x786E;&#x4FDD;&#x7C7B;&#x578B;&#x53D8;&#x91CF;<code>K extends keyof T</code>&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;
&#x4F8B;&#x5982;&#x4E0B;&#x9762;<code>getProperty</code>&#x51FD;&#x6570;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-ts">function getProperty&lt;T, K extends keyof T&gt;(o: T, name: K): T[K] {
    return o[name]; // o[name] is of type T[K]
}
</code></pre>
<p><code>getProperty</code>&#x91CC;&#x7684;<code>o: T</code>&#x548C;<code>name: K</code>&#xFF0C;&#x610F;&#x5473;&#x7740;<code>o[name]: T[K]</code>&#x3002;
&#x5F53;&#x4F60;&#x8FD4;&#x56DE;<code>T[K]</code>&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x5B9E;&#x4F8B;&#x5316;&#x952E;&#x7684;&#x771F;&#x5B9E;&#x7C7B;&#x578B;&#xFF0C;&#x56E0;&#x6B64;<code>getProperty</code>&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x4F1A;&#x968F;&#x7740;&#x4F60;&#x9700;&#x8981;&#x7684;&#x5C5E;&#x6027;&#x6539;&#x53D8;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> name: <span class="hljs-built_in">string</span> = getProperty(person, <span class="hljs-string">&apos;name&apos;</span>);
<span class="hljs-keyword">let</span> age: <span class="hljs-built_in">number</span> = getProperty(person, <span class="hljs-string">&apos;age&apos;</span>);
<span class="hljs-keyword">let</span> unknown = getProperty(person, <span class="hljs-string">&apos;unknown&apos;</span>); <span class="hljs-comment">// error, &apos;unknown&apos; is not in &apos;name&apos; | &apos;age&apos;</span>
</code></pre>
<h2 id="&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x7D22;&#x5F15;&#x7B7E;&#x540D;">&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x7D22;&#x5F15;&#x7B7E;&#x540D;</h2>
<p><code>keyof</code>&#x548C;<code>T[K]</code>&#x4E0E;&#x5B57;&#x7B26;&#x4E32;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x8FDB;&#x884C;&#x4EA4;&#x4E92;&#x3002;
&#x5982;&#x679C;&#x4F60;&#x6709;&#x4E00;&#x4E2A;&#x5E26;&#x6709;&#x5B57;&#x7B26;&#x4E32;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x90A3;&#x4E48;<code>keyof T</code>&#x4F1A;&#x662F;<code>string</code>&#x3002;
&#x5E76;&#x4E14;<code>T[string]</code>&#x4E3A;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x7684;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Map&lt;T&gt; {
    [key: <span class="hljs-built_in">string</span>]: T;
}
<span class="hljs-keyword">let</span> keys: keyof Map&lt;<span class="hljs-built_in">number</span>&gt;; <span class="hljs-comment">// string</span>
<span class="hljs-keyword">let</span> value: Map&lt;<span class="hljs-built_in">number</span>&gt;[<span class="hljs-string">&apos;foo&apos;</span>]; <span class="hljs-comment">// number</span>
</code></pre>
<h1 id="&#x6620;&#x5C04;&#x7C7B;&#x578B;">&#x6620;&#x5C04;&#x7C7B;&#x578B;</h1>
<p>&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x7684;&#x4EFB;&#x52A1;&#x662F;&#x5C06;&#x4E00;&#x4E2A;&#x5DF2;&#x77E5;&#x7684;&#x7C7B;&#x578B;&#x6BCF;&#x4E2A;&#x5C5E;&#x6027;&#x90FD;&#x53D8;&#x4E3A;&#x53EF;&#x9009;&#x7684;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> PersonPartial {
    name?: <span class="hljs-built_in">string</span>;
    age?: <span class="hljs-built_in">number</span>;
}
</code></pre>
<p>&#x6216;&#x8005;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x4E00;&#x4E2A;&#x53EA;&#x8BFB;&#x7248;&#x672C;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> PersonReadonly {
    readonly name: <span class="hljs-built_in">string</span>;
    readonly age: <span class="hljs-built_in">number</span>;
}
</code></pre>
<p>&#x8FD9;&#x5728;JavaScript&#x91CC;&#x7ECF;&#x5E38;&#x51FA;&#x73B0;&#xFF0C;TypeScript&#x63D0;&#x4F9B;&#x4E86;&#x4ECE;&#x65E7;&#x7C7B;&#x578B;&#x4E2D;&#x521B;&#x5EFA;&#x65B0;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x79CD;&#x65B9;&#x5F0F; &#x2014; <strong>&#x6620;&#x5C04;&#x7C7B;&#x578B;</strong>&#x3002;
&#x5728;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x91CC;&#xFF0C;&#x65B0;&#x7C7B;&#x578B;&#x4EE5;&#x76F8;&#x540C;&#x7684;&#x5F62;&#x5F0F;&#x53BB;&#x8F6C;&#x6362;&#x65E7;&#x7C7B;&#x578B;&#x91CC;&#x6BCF;&#x4E2A;&#x5C5E;&#x6027;&#x3002;
&#x4F8B;&#x5982;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4EE4;&#x6BCF;&#x4E2A;&#x5C5E;&#x6027;&#x6210;&#x4E3A;<code>readonly</code>&#x7C7B;&#x578B;&#x6216;&#x53EF;&#x9009;&#x7684;&#x3002;
&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E9B;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Readonly&lt;T&gt; = {
    readonly [P <span class="hljs-keyword">in</span> keyof T]: T[P];
}
<span class="hljs-keyword">type</span> Partial&lt;T&gt; = {
    [P <span class="hljs-keyword">in</span> keyof T]?: T[P];
}
</code></pre>
<p>&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> PersonPartial = Partial&lt;Person&gt;;
<span class="hljs-keyword">type</span> ReadonlyPerson = Readonly&lt;Person&gt;;
</code></pre>
<p>&#x4E0B;&#x9762;&#x6765;&#x770B;&#x770B;&#x6700;&#x7B80;&#x5355;&#x7684;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x548C;&#x5B83;&#x7684;&#x7EC4;&#x6210;&#x90E8;&#x5206;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Keys = <span class="hljs-string">&apos;option1&apos;</span> | <span class="hljs-string">&apos;option2&apos;</span>;
<span class="hljs-keyword">type</span> Flags = { [K <span class="hljs-keyword">in</span> Keys]: <span class="hljs-built_in">boolean</span> };
</code></pre>
<p>&#x5B83;&#x7684;&#x8BED;&#x6CD5;&#x4E0E;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x7684;&#x8BED;&#x6CD5;&#x7C7B;&#x578B;&#xFF0C;&#x5185;&#x90E8;&#x4F7F;&#x7528;&#x4E86;<code>for .. in</code>&#x3002;
&#x5177;&#x6709;&#x4E09;&#x4E2A;&#x90E8;&#x5206;&#xFF1A;</p>
<ol>
<li>&#x7C7B;&#x578B;&#x53D8;&#x91CF;<code>K</code>&#xFF0C;&#x5B83;&#x4F1A;&#x4F9D;&#x6B21;&#x7ED1;&#x5B9A;&#x5230;&#x6BCF;&#x4E2A;&#x5C5E;&#x6027;&#x3002;</li>
<li>&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x8054;&#x5408;&#x7684;<code>Keys</code>&#xFF0C;&#x5B83;&#x5305;&#x542B;&#x4E86;&#x8981;&#x8FED;&#x4EE3;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x7684;&#x96C6;&#x5408;&#x3002;</li>
<li>&#x5C5E;&#x6027;&#x7684;&#x7ED3;&#x679C;&#x7C7B;&#x578B;&#x3002;</li>
</ol>
<p>&#x5728;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;<code>Keys</code>&#x662F;&#x786C;&#x7F16;&#x7801;&#x7684;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x5217;&#x8868;&#x5E76;&#x4E14;&#x5C5E;&#x6027;&#x7C7B;&#x578B;&#x6C38;&#x8FDC;&#x662F;<code>boolean</code>&#xFF0C;&#x56E0;&#x6B64;&#x8FD9;&#x4E2A;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x7B49;&#x540C;&#x4E8E;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Flags = {
    option1: <span class="hljs-built_in">boolean</span>;
    option2: <span class="hljs-built_in">boolean</span>;
}
</code></pre>
<p>&#x5728;&#x771F;&#x6B63;&#x7684;&#x5E94;&#x7528;&#x91CC;&#xFF0C;&#x53EF;&#x80FD;&#x4E0D;&#x540C;&#x4E8E;&#x4E0A;&#x9762;&#x7684;<code>Readonly</code>&#x6216;<code>Partial</code>&#x3002;
&#x5B83;&#x4EEC;&#x4F1A;&#x57FA;&#x4E8E;&#x4E00;&#x4E9B;&#x5DF2;&#x5B58;&#x5728;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4E14;&#x6309;&#x7167;&#x4E00;&#x5B9A;&#x7684;&#x65B9;&#x5F0F;&#x8F6C;&#x6362;&#x5B57;&#x6BB5;&#x3002;
&#x8FD9;&#x5C31;&#x662F;<code>keyof</code>&#x548C;&#x7D22;&#x5F15;&#x8BBF;&#x95EE;&#x7C7B;&#x578B;&#x8981;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> NullablePerson = { [P <span class="hljs-keyword">in</span> keyof Person]: Person[P] | <span class="hljs-literal">null</span> }
<span class="hljs-keyword">type</span> PartialPerson = { [P <span class="hljs-keyword">in</span> keyof Person]?: Person[P] }
</code></pre>
<p>&#x4F46;&#x5B83;&#x66F4;&#x6709;&#x7528;&#x7684;&#x5730;&#x65B9;&#x662F;&#x53EF;&#x4EE5;&#x6709;&#x4E00;&#x4E9B;&#x901A;&#x7528;&#x7248;&#x672C;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Nullable&lt;T&gt; = { [P <span class="hljs-keyword">in</span> keyof T]: T[P] | <span class="hljs-literal">null</span> }
<span class="hljs-keyword">type</span> Partial&lt;T&gt; = { [P <span class="hljs-keyword">in</span> keyof T]?: T[P] }
</code></pre>
<p>&#x5728;&#x8FD9;&#x4E9B;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;&#x5C5E;&#x6027;&#x5217;&#x8868;&#x662F;<code>keyof T</code>&#x4E14;&#x7ED3;&#x679C;&#x7C7B;&#x578B;&#x662F;<code>T[P]</code>&#x7684;&#x53D8;&#x4F53;&#x3002;
&#x8FD9;&#x662F;&#x4F7F;&#x7528;&#x901A;&#x7528;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x4E2A;&#x597D;&#x6A21;&#x7248;&#x3002;
&#x56E0;&#x4E3A;&#x8FD9;&#x7C7B;&#x8F6C;&#x6362;&#x662F;<a href="https://en.wikipedia.org/wiki/Homomorphism" target="_blank">&#x540C;&#x6001;</a>&#x7684;&#xFF0C;&#x6620;&#x5C04;&#x53EA;&#x4F5C;&#x7528;&#x4E8E;<code>T</code>&#x7684;&#x5C5E;&#x6027;&#x800C;&#x6CA1;&#x6709;&#x5176;&#x5B83;&#x7684;&#x3002;
&#x7F16;&#x8BD1;&#x5668;&#x77E5;&#x9053;&#x5728;&#x6DFB;&#x52A0;&#x4EFB;&#x4F55;&#x65B0;&#x5C5E;&#x6027;&#x4E4B;&#x524D;&#x53EF;&#x4EE5;&#x62F7;&#x8D1D;&#x6240;&#x6709;&#x5B58;&#x5728;&#x7684;&#x5C5E;&#x6027;&#x4FEE;&#x9970;&#x7B26;&#x3002;
&#x4F8B;&#x5982;&#xFF0C;&#x5047;&#x8BBE;<code>Person.name</code>&#x662F;&#x53EA;&#x8BFB;&#x7684;&#xFF0C;&#x90A3;&#x4E48;<code>Partial&lt;Person&gt;.name</code>&#x4E5F;&#x5C06;&#x662F;&#x53EA;&#x8BFB;&#x7684;&#x4E14;&#x4E3A;&#x53EF;&#x9009;&#x7684;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x53E6;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;<code>T[P]</code>&#x88AB;&#x5305;&#x88C5;&#x5728;<code>Proxy&lt;T&gt;</code>&#x7C7B;&#x91CC;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Proxy&lt;T&gt; = {
    <span class="hljs-keyword">get</span>(): T;
    <span class="hljs-keyword">set</span>(value: T): <span class="hljs-built_in">void</span>;
}
<span class="hljs-keyword">type</span> Proxify&lt;T&gt; = {
    [P <span class="hljs-keyword">in</span> keyof T]: Proxy&lt;T[P]&gt;;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">proxify</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">o: T</span>): <span class="hljs-title">Proxify</span>&lt;<span class="hljs-title">T</span>&gt; </span>{
   <span class="hljs-comment">// ... wrap proxies ...</span>
}
<span class="hljs-keyword">let</span> proxyProps = proxify(props);
</code></pre>
<p>&#x6CE8;&#x610F;<code>Readonly&lt;T&gt;</code>&#x548C;<code>Partial&lt;T&gt;</code>&#x7528;&#x5904;&#x4E0D;&#x5C0F;&#xFF0C;&#x56E0;&#x6B64;&#x5B83;&#x4EEC;&#x4E0E;<code>Pick</code>&#x548C;<code>Record</code>&#x4E00;&#x540C;&#x88AB;&#x5305;&#x542B;&#x8FDB;&#x4E86;TypeScript&#x7684;&#x6807;&#x51C6;&#x5E93;&#x91CC;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Pick&lt;T, K extends keyof T&gt; = {
    [P <span class="hljs-keyword">in</span> K]: T[P];
}
<span class="hljs-keyword">type</span> Record&lt;K extends <span class="hljs-built_in">string</span>, T&gt; = {
    [P <span class="hljs-keyword">in</span> K]: T;
}
</code></pre>
<p><code>Readonly</code>&#xFF0C;<code>Partial</code>&#x548C;<code>Pick</code>&#x662F;&#x540C;&#x6001;&#x7684;&#xFF0C;&#x4F46;<code>Record</code>&#x4E0D;&#x662F;&#x3002;
&#x56E0;&#x4E3A;<code>Record</code>&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x8F93;&#x5165;&#x7C7B;&#x578B;&#x6765;&#x62F7;&#x8D1D;&#x5C5E;&#x6027;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x4E0D;&#x5C5E;&#x4E8E;&#x540C;&#x6001;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> ThreeStringProps = Record&lt;<span class="hljs-string">&apos;prop1&apos;</span> | <span class="hljs-string">&apos;prop2&apos;</span> | <span class="hljs-string">&apos;prop3&apos;</span>, <span class="hljs-built_in">string</span>&gt;
</code></pre>
<p>&#x975E;&#x540C;&#x6001;&#x7C7B;&#x578B;&#x672C;&#x8D28;&#x4E0A;&#x4F1A;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x56E0;&#x6B64;&#x5B83;&#x4EEC;&#x4E0D;&#x4F1A;&#x4ECE;&#x5B83;&#x5904;&#x62F7;&#x8D1D;&#x5C5E;&#x6027;&#x4FEE;&#x9970;&#x7B26;&#x3002;</p>
<h2 id="&#x7531;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x63A8;&#x65AD;">&#x7531;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x63A8;&#x65AD;</h2>
<p>&#x73B0;&#x5728;&#x4F60;&#x4E86;&#x89E3;&#x4E86;&#x5982;&#x4F55;&#x5305;&#x88C5;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x90A3;&#x4E48;&#x63A5;&#x4E0B;&#x6765;&#x5C31;&#x662F;&#x5982;&#x4F55;&#x62C6;&#x5305;&#x3002;
&#x5176;&#x5B9E;&#x8FD9;&#x4E5F;&#x975E;&#x5E38;&#x5BB9;&#x6613;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">unproxify</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">t: Proxify&lt;T&gt;</span>): <span class="hljs-title">T</span> </span>{
    <span class="hljs-keyword">let</span> result = {} as T;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> k <span class="hljs-keyword">in</span> t) {
        result[k] = t[k].get();
    }
    <span class="hljs-keyword">return</span> result;
}

<span class="hljs-keyword">let</span> originalProps = unproxify(proxyProps);
</code></pre>
<p>&#x6CE8;&#x610F;&#x8FD9;&#x4E2A;&#x62C6;&#x5305;&#x63A8;&#x65AD;&#x53EA;&#x9002;&#x7528;&#x4E8E;&#x540C;&#x6001;&#x7684;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x3002;
&#x5982;&#x679C;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x4E0D;&#x662F;&#x540C;&#x6001;&#x7684;&#xFF0C;&#x90A3;&#x4E48;&#x9700;&#x8981;&#x7ED9;&#x62C6;&#x5305;&#x51FD;&#x6570;&#x4E00;&#x4E2A;&#x660E;&#x786E;&#x7684;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x3002;</p>
<h2 id="&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;">&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;</h2>
<p>TypeScript 2.8&#x5F15;&#x5165;&#x4E86;<em>&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;</em>&#xFF0C;&#x5B83;&#x80FD;&#x591F;&#x8868;&#x793A;&#x975E;&#x7EDF;&#x4E00;&#x7684;&#x7C7B;&#x578B;&#x3002;
&#x6709;&#x6761;&#x4EF6;&#x7684;&#x7C7B;&#x578B;&#x4F1A;&#x4EE5;&#x4E00;&#x4E2A;&#x6761;&#x4EF6;&#x8868;&#x8FBE;&#x5F0F;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x5173;&#x7CFB;&#x68C0;&#x6D4B;&#xFF0C;&#x4ECE;&#x800C;&#x5728;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#x4E2D;&#x9009;&#x62E9;&#x5176;&#x4E00;&#xFF1A;</p>
<pre><code class="lang-ts">T extends U ? X : Y
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x7C7B;&#x578B;&#x610F;&#x601D;&#x662F;&#xFF0C;&#x82E5;<code>T</code>&#x80FD;&#x591F;&#x8D4B;&#x503C;&#x7ED9;<code>U</code>&#xFF0C;&#x90A3;&#x4E48;&#x7C7B;&#x578B;&#x662F;<code>X</code>&#xFF0C;&#x5426;&#x5219;&#x4E3A;<code>Y</code>&#x3002;</p>
<p>&#x6709;&#x6761;&#x4EF6;&#x7684;&#x7C7B;&#x578B;<code>T extends U ? X : Y</code>&#x6216;&#x8005;<em>&#x89E3;&#x6790;</em>&#x4E3A;<code>X</code>&#xFF0C;&#x6216;&#x8005;<em>&#x89E3;&#x6790;</em>&#x4E3A;<code>Y</code>&#xFF0C;&#x518D;&#x6216;&#x8005;<em>&#x5EF6;&#x8FDF;</em>&#x89E3;&#x6790;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x53EF;&#x80FD;&#x4F9D;&#x8D56;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#x3002;
&#x82E5;<code>T</code>&#x6216;<code>U</code>&#x5305;&#x542B;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x662F;&#x5426;&#x89E3;&#x6790;&#x4E3A;<code>X</code>&#x6216;<code>Y</code>&#x6216;&#x63A8;&#x8FDF;&#xFF0C;&#x53D6;&#x51B3;&#x4E8E;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x662F;&#x5426;&#x6709;&#x8DB3;&#x591F;&#x7684;&#x4FE1;&#x606F;&#x6765;&#x786E;&#x5B9A;<code>T</code>&#x603B;&#x662F;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;<code>U</code>&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E9B;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x88AB;&#x7ACB;&#x5373;&#x89E3;&#x6790;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>&lt;<span class="hljs-title">T</span> <span class="hljs-title">extends</span> <span class="hljs-title">boolean</span>&gt;(<span class="hljs-params">x: T</span>): <span class="hljs-title">T</span> <span class="hljs-title">extends</span> <span class="hljs-title">true</span> ? <span class="hljs-title">string</span> : <span class="hljs-title">number</span></span>;

<span class="hljs-comment">// Type is &apos;string | number</span>
<span class="hljs-keyword">let</span> x = f(<span class="hljs-built_in">Math</span>.random() &lt; <span class="hljs-number">0.5</span>)
</code></pre>
<p>&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x6D89;&#x53CA;<code>TypeName</code>&#x7C7B;&#x578B;&#x522B;&#x540D;&#xFF0C;&#x5B83;&#x4F7F;&#x7528;&#x4E86;&#x5D4C;&#x5957;&#x4E86;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> TypeName&lt;T&gt; =
    T extends <span class="hljs-built_in">string</span> ? <span class="hljs-string">&quot;string&quot;</span> :
    T extends <span class="hljs-built_in">number</span> ? <span class="hljs-string">&quot;number&quot;</span> :
    T extends <span class="hljs-built_in">boolean</span> ? <span class="hljs-string">&quot;boolean&quot;</span> :
    T extends <span class="hljs-literal">undefined</span> ? <span class="hljs-string">&quot;undefined&quot;</span> :
    T extends <span class="hljs-built_in">Function</span> ? <span class="hljs-string">&quot;function&quot;</span> :
    <span class="hljs-string">&quot;object&quot;</span>;

<span class="hljs-keyword">type</span> T0 = TypeName&lt;<span class="hljs-built_in">string</span>&gt;;  <span class="hljs-comment">// &quot;string&quot;</span>
<span class="hljs-keyword">type</span> T1 = TypeName&lt;<span class="hljs-string">&quot;a&quot;</span>&gt;;  <span class="hljs-comment">// &quot;string&quot;</span>
<span class="hljs-keyword">type</span> T2 = TypeName&lt;<span class="hljs-literal">true</span>&gt;;  <span class="hljs-comment">// &quot;boolean&quot;</span>
<span class="hljs-keyword">type</span> T3 = TypeName&lt;() =&gt; <span class="hljs-built_in">void</span>&gt;;  <span class="hljs-comment">// &quot;function&quot;</span>
<span class="hljs-keyword">type</span> T4 = TypeName&lt;<span class="hljs-built_in">string</span>[]&gt;;  <span class="hljs-comment">// &quot;object&quot;</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x88AB;&#x63A8;&#x8FDF;&#x89E3;&#x6790;&#x7684;&#x4F8B;&#x5B50;:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Foo {
    propA: <span class="hljs-built_in">boolean</span>;
    propB: <span class="hljs-built_in">boolean</span>;
}

<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">x: T</span>): <span class="hljs-title">T</span> <span class="hljs-title">extends</span> <span class="hljs-title">Foo</span> ? <span class="hljs-title">string</span> : <span class="hljs-title">number</span></span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>&lt;<span class="hljs-title">U</span>&gt;(<span class="hljs-params">x: U</span>) </span>{
    <span class="hljs-comment">// Has type &apos;U extends Foo ? string : number&apos;</span>
    <span class="hljs-keyword">let</span> a = f(x);

    <span class="hljs-comment">// This assignment is allowed though!</span>
    <span class="hljs-keyword">let</span> b: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span> = a;
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#xFF0C;<code>a</code>&#x53D8;&#x91CF;&#x542B;&#x6709;&#x672A;&#x786E;&#x5B9A;&#x7684;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x3002;
&#x5F53;&#x6709;&#x53E6;&#x4E00;&#x6BB5;&#x4EE3;&#x7801;&#x8C03;&#x7528;<code>foo</code>&#xFF0C;&#x5B83;&#x4F1A;&#x7528;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#x66FF;&#x6362;<code>U</code>&#xFF0C;TypeScript&#x5C06;&#x91CD;&#x65B0;&#x8BA1;&#x7B97;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#xFF0C;&#x51B3;&#x5B9A;&#x5B83;&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x4E00;&#x4E2A;&#x5206;&#x652F;&#x3002;</p>
<p>&#x4E0E;&#x6B64;&#x540C;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x8D4B;&#x503C;&#x7ED9;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#xFF0C;&#x53EA;&#x8981;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x7684;&#x6BCF;&#x4E2A;&#x5206;&#x652F;&#x90FD;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;&#x76EE;&#x6807;&#x7C7B;&#x578B;&#x3002;
&#x56E0;&#x6B64;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;<code>U extends Foo ? string : number</code>&#x8D4B;&#x503C;&#x7ED9;<code>string | number</code>&#xFF0C;&#x56E0;&#x4E3A;&#x4E0D;&#x7BA1;&#x8FD9;&#x4E2A;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x6700;&#x7EC8;&#x7ED3;&#x679C;&#x662F;&#x4EC0;&#x4E48;&#xFF0C;&#x5B83;&#x53EA;&#x80FD;&#x662F;<code>string</code>&#x6216;<code>number</code>&#x3002;</p>
<h3 id="&#x5206;&#x5E03;&#x5F0F;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;">&#x5206;&#x5E03;&#x5F0F;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;</h3>
<p>&#x5982;&#x679C;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x91CC;&#x5F85;&#x68C0;&#x67E5;&#x7684;&#x7C7B;&#x578B;&#x662F;<code>naked type parameter</code>&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x4E5F;&#x88AB;&#x79F0;&#x4E3A;&#x201C;&#x5206;&#x5E03;&#x5F0F;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x201D;&#x3002;
&#x5206;&#x5E03;&#x5F0F;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x5728;&#x5B9E;&#x4F8B;&#x5316;&#x65F6;&#x4F1A;&#x81EA;&#x52A8;&#x5206;&#x53D1;&#x6210;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x3002;
&#x4F8B;&#x5982;&#xFF0C;&#x5B9E;&#x4F8B;&#x5316;<code>T extends U ? X : Y</code>&#xFF0C;<code>T</code>&#x7684;&#x7C7B;&#x578B;&#x4E3A;<code>A | B | C</code>&#xFF0C;&#x4F1A;&#x88AB;&#x89E3;&#x6790;&#x4E3A;<code>(A extends U ? X : Y) | (B extends U ? X : Y) | (C extends U ? X : Y)</code>&#x3002;</p>
<h4 id="&#x4F8B;&#x5B50;">&#x4F8B;&#x5B50;</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> T10 = TypeName&lt;<span class="hljs-built_in">string</span> | (() =&gt; <span class="hljs-built_in">void</span>)&gt;;  <span class="hljs-comment">// &quot;string&quot; | &quot;function&quot;</span>
<span class="hljs-keyword">type</span> T12 = TypeName&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">string</span>[] | <span class="hljs-literal">undefined</span>&gt;;  <span class="hljs-comment">// &quot;string&quot; | &quot;object&quot; | &quot;undefined&quot;</span>
<span class="hljs-keyword">type</span> T11 = TypeName&lt;<span class="hljs-built_in">string</span>[] | <span class="hljs-built_in">number</span>[]&gt;;  <span class="hljs-comment">// &quot;object&quot;</span>
</code></pre>
<p>&#x5728;<code>T extends U ? X : Y</code>&#x7684;&#x5B9E;&#x4F8B;&#x5316;&#x91CC;&#xFF0C;&#x5BF9;<code>T</code>&#x7684;&#x5F15;&#x7528;&#x88AB;&#x89E3;&#x6790;&#x4E3A;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF08;&#x6BD4;&#x5982;&#xFF0C;<code>T</code>&#x6307;&#x5411;&#x67D0;&#x4E00;&#x5355;&#x4E2A;&#x90E8;&#x5206;&#xFF0C;&#x5728;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x5206;&#x5E03;&#x5230;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x4E4B;&#x540E;&#xFF09;&#x3002;
&#x6B64;&#x5916;&#xFF0C;&#x5728;<code>X</code>&#x5185;&#x5BF9;<code>T</code>&#x7684;&#x5F15;&#x7528;&#x6709;&#x4E00;&#x4E2A;&#x9644;&#x52A0;&#x7684;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x7EA6;&#x675F;<code>U</code>&#xFF08;&#x4F8B;&#x5982;&#xFF0C;<code>T</code>&#x88AB;&#x5F53;&#x6210;&#x5728;<code>X</code>&#x5185;&#x53EF;&#x8D4B;&#x503C;&#x7ED9;<code>U</code>&#xFF09;&#x3002;</p>
<h4 id="&#x4F8B;&#x5B50;">&#x4F8B;&#x5B50;</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> BoxedValue&lt;T&gt; = { value: T };
<span class="hljs-keyword">type</span> BoxedArray&lt;T&gt; = { array: T[] };
<span class="hljs-keyword">type</span> Boxed&lt;T&gt; = T extends <span class="hljs-built_in">any</span>[] ? BoxedArray&lt;T[<span class="hljs-built_in">number</span>]&gt; : BoxedValue&lt;T&gt;;

<span class="hljs-keyword">type</span> T20 = Boxed&lt;<span class="hljs-built_in">string</span>&gt;;  <span class="hljs-comment">// BoxedValue&lt;string&gt;;</span>
<span class="hljs-keyword">type</span> T21 = Boxed&lt;<span class="hljs-built_in">number</span>[]&gt;;  <span class="hljs-comment">// BoxedArray&lt;number&gt;;</span>
<span class="hljs-keyword">type</span> T22 = Boxed&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span>[]&gt;;  <span class="hljs-comment">// BoxedValue&lt;string&gt; | BoxedArray&lt;number&gt;;</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#x5728;<code>Boxed&lt;T&gt;</code>&#x7684;<code>true</code>&#x5206;&#x652F;&#x91CC;&#xFF0C;<code>T</code>&#x6709;&#x4E2A;&#x989D;&#x5916;&#x7684;&#x7EA6;&#x675F;<code>any[]</code>&#xFF0C;&#x56E0;&#x6B64;&#x5B83;&#x9002;&#x7528;&#x4E8E;<code>T[number]</code>&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x3002;&#x540C;&#x65F6;&#x4E5F;&#x6CE8;&#x610F;&#x4E00;&#x4E0B;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x662F;&#x5982;&#x4F55;&#x5206;&#x5E03;&#x6210;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x3002;</p>
<p>&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x7684;&#x5206;&#x5E03;&#x5F0F;&#x7684;&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x5730;&#x7528;&#x6765;<em>&#x8FC7;&#x6EE4;</em>&#x8054;&#x5408;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Diff&lt;T, U&gt; = T extends U ? never : T;  <span class="hljs-comment">// Remove types from T that are assignable to U</span>
<span class="hljs-keyword">type</span> Filter&lt;T, U&gt; = T extends U ? T : never;  <span class="hljs-comment">// Remove types from T that are not assignable to U</span>

<span class="hljs-keyword">type</span> T30 = Diff&lt;<span class="hljs-string">&quot;a&quot;</span> | <span class="hljs-string">&quot;b&quot;</span> | <span class="hljs-string">&quot;c&quot;</span> | <span class="hljs-string">&quot;d&quot;</span>, <span class="hljs-string">&quot;a&quot;</span> | <span class="hljs-string">&quot;c&quot;</span> | <span class="hljs-string">&quot;f&quot;</span>&gt;;  <span class="hljs-comment">// &quot;b&quot; | &quot;d&quot;</span>
<span class="hljs-keyword">type</span> T31 = Filter&lt;<span class="hljs-string">&quot;a&quot;</span> | <span class="hljs-string">&quot;b&quot;</span> | <span class="hljs-string">&quot;c&quot;</span> | <span class="hljs-string">&quot;d&quot;</span>, <span class="hljs-string">&quot;a&quot;</span> | <span class="hljs-string">&quot;c&quot;</span> | <span class="hljs-string">&quot;f&quot;</span>&gt;;  <span class="hljs-comment">// &quot;a&quot; | &quot;c&quot;</span>
<span class="hljs-keyword">type</span> T32 = Diff&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span> | (() =&gt; <span class="hljs-built_in">void</span>), <span class="hljs-built_in">Function</span>&gt;;  <span class="hljs-comment">// string | number</span>
<span class="hljs-keyword">type</span> T33 = Filter&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span> | (() =&gt; <span class="hljs-built_in">void</span>), <span class="hljs-built_in">Function</span>&gt;;  <span class="hljs-comment">// () =&gt; void</span>

<span class="hljs-keyword">type</span> NonNullable&lt;T&gt; = Diff&lt;T, <span class="hljs-literal">null</span> | <span class="hljs-literal">undefined</span>&gt;;  <span class="hljs-comment">// Remove null and undefined from T</span>

<span class="hljs-keyword">type</span> T34 = NonNullable&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span> | <span class="hljs-literal">undefined</span>&gt;;  <span class="hljs-comment">// string | number</span>
<span class="hljs-keyword">type</span> T35 = NonNullable&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">string</span>[] | <span class="hljs-literal">null</span> | <span class="hljs-literal">undefined</span>&gt;;  <span class="hljs-comment">// string | string[]</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f1</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">x: T, y: NonNullable&lt;T&gt;</span>) </span>{
    x = y;  <span class="hljs-comment">// Ok</span>
    y = x;  <span class="hljs-comment">// Error</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f2</span>&lt;<span class="hljs-title">T</span> <span class="hljs-title">extends</span> <span class="hljs-title">string</span> | <span class="hljs-title">undefined</span>&gt;(<span class="hljs-params">x: T, y: NonNullable&lt;T&gt;</span>) </span>{
    x = y;  <span class="hljs-comment">// Ok</span>
    y = x;  <span class="hljs-comment">// Error</span>
    <span class="hljs-keyword">let</span> s1: <span class="hljs-built_in">string</span> = x;  <span class="hljs-comment">// Error</span>
    <span class="hljs-keyword">let</span> s2: <span class="hljs-built_in">string</span> = y;  <span class="hljs-comment">// Ok</span>
}
</code></pre>
<p>&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x4E0E;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x7ED3;&#x5408;&#x65F6;&#x7279;&#x522B;&#x6709;&#x7528;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> FunctionPropertyNames&lt;T&gt; = { [K <span class="hljs-keyword">in</span> keyof T]: T[K] extends <span class="hljs-built_in">Function</span> ? K : never }[keyof T];
<span class="hljs-keyword">type</span> FunctionProperties&lt;T&gt; = Pick&lt;T, FunctionPropertyNames&lt;T&gt;&gt;;

<span class="hljs-keyword">type</span> NonFunctionPropertyNames&lt;T&gt; = { [K <span class="hljs-keyword">in</span> keyof T]: T[K] extends <span class="hljs-built_in">Function</span> ? never : K }[keyof T];
<span class="hljs-keyword">type</span> NonFunctionProperties&lt;T&gt; = Pick&lt;T, NonFunctionPropertyNames&lt;T&gt;&gt;;

<span class="hljs-keyword">interface</span> Part {
    id: <span class="hljs-built_in">number</span>;
    name: <span class="hljs-built_in">string</span>;
    subparts: Part[];
    updatePart(newName: <span class="hljs-built_in">string</span>): <span class="hljs-built_in">void</span>;
}

<span class="hljs-keyword">type</span> T40 = FunctionPropertyNames&lt;Part&gt;;  <span class="hljs-comment">// &quot;updatePart&quot;</span>
<span class="hljs-keyword">type</span> T41 = NonFunctionPropertyNames&lt;Part&gt;;  <span class="hljs-comment">// &quot;id&quot; | &quot;name&quot; | &quot;subparts&quot;</span>
<span class="hljs-keyword">type</span> T42 = FunctionProperties&lt;Part&gt;;  <span class="hljs-comment">// { updatePart(newName: string): void }</span>
<span class="hljs-keyword">type</span> T43 = NonFunctionProperties&lt;Part&gt;;  <span class="hljs-comment">// { id: number, name: string, subparts: Part[] }</span>
</code></pre>
<p>&#x4E0E;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x548C;&#x4EA4;&#x53C9;&#x7C7B;&#x578B;&#x76F8;&#x4F3C;&#xFF0C;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x4E0D;&#x5141;&#x8BB8;&#x9012;&#x5F52;&#x5730;&#x5F15;&#x7528;&#x81EA;&#x5DF1;&#x3002;&#x6BD4;&#x5982;&#x4E0B;&#x9762;&#x7684;&#x9519;&#x8BEF;&#x3002;</p>
<h4 id="&#x4F8B;&#x5B50;">&#x4F8B;&#x5B50;</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> ElementType&lt;T&gt; = T extends <span class="hljs-built_in">any</span>[] ? ElementType&lt;T[<span class="hljs-built_in">number</span>]&gt; : T;  <span class="hljs-comment">// Error</span>
</code></pre>
<h3 id="&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x4E2D;&#x7684;&#x7C7B;&#x578B;&#x63A8;&#x65AD;">&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x4E2D;&#x7684;&#x7C7B;&#x578B;&#x63A8;&#x65AD;</h3>
<p>&#x73B0;&#x5728;&#x5728;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x7684;<code>extends</code>&#x5B50;&#x8BED;&#x53E5;&#x4E2D;&#xFF0C;&#x5141;&#x8BB8;&#x51FA;&#x73B0;<code>infer</code>&#x58F0;&#x660E;&#xFF0C;&#x5B83;&#x4F1A;&#x5F15;&#x5165;&#x4E00;&#x4E2A;&#x5F85;&#x63A8;&#x65AD;&#x7684;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#x3002;
&#x8FD9;&#x4E2A;&#x63A8;&#x65AD;&#x7684;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x5728;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x7684;true&#x5206;&#x652F;&#x4E2D;&#x88AB;&#x5F15;&#x7528;&#x3002;
&#x5141;&#x8BB8;&#x51FA;&#x73B0;&#x591A;&#x4E2A;&#x540C;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#x7684;<code>infer</code>&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x4F1A;&#x63D0;&#x53D6;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> ReturnType&lt;T&gt; = T extends (...args: <span class="hljs-built_in">any</span>[]) =&gt; infer R ? R : <span class="hljs-built_in">any</span>;
</code></pre>
<p>&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x5D4C;&#x5957;&#x6765;&#x6784;&#x6210;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x5339;&#x914D;&#x6A21;&#x5F0F;&#xFF0C;&#x6309;&#x987A;&#x5E8F;&#x8FDB;&#x884C;&#x6C42;&#x503C;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Unpacked&lt;T&gt; =
    T extends (infer U)[] ? U :
    T extends (...args: <span class="hljs-built_in">any</span>[]) =&gt; infer U ? U :
    T extends Promise&lt;infer U&gt; ? U :
    T;

<span class="hljs-keyword">type</span> T0 = Unpacked&lt;<span class="hljs-built_in">string</span>&gt;;  <span class="hljs-comment">// string</span>
<span class="hljs-keyword">type</span> T1 = Unpacked&lt;<span class="hljs-built_in">string</span>[]&gt;;  <span class="hljs-comment">// string</span>
<span class="hljs-keyword">type</span> T2 = Unpacked&lt;() =&gt; <span class="hljs-built_in">string</span>&gt;;  <span class="hljs-comment">// string</span>
<span class="hljs-keyword">type</span> T3 = Unpacked&lt;Promise&lt;<span class="hljs-built_in">string</span>&gt;&gt;;  <span class="hljs-comment">// string</span>
<span class="hljs-keyword">type</span> T4 = Unpacked&lt;Promise&lt;<span class="hljs-built_in">string</span>&gt;[]&gt;;  <span class="hljs-comment">// Promise&lt;string&gt;</span>
<span class="hljs-keyword">type</span> T5 = Unpacked&lt;Unpacked&lt;Promise&lt;<span class="hljs-built_in">string</span>&gt;[]&gt;&gt;;  <span class="hljs-comment">// string</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x89E3;&#x91CA;&#x4E86;&#x5728;&#x534F;&#x53D8;&#x4F4D;&#x7F6E;&#x4E0A;&#xFF0C;&#x540C;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#x7684;&#x591A;&#x4E2A;&#x5019;&#x9009;&#x7C7B;&#x578B;&#x4F1A;&#x88AB;&#x63A8;&#x65AD;&#x4E3A;&#x8054;&#x5408;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Foo&lt;T&gt; = T extends { a: infer U, b: infer U } ? U : never;
<span class="hljs-keyword">type</span> T10 = Foo&lt;{ a: <span class="hljs-built_in">string</span>, b: <span class="hljs-built_in">string</span> }&gt;;  <span class="hljs-comment">// string</span>
<span class="hljs-keyword">type</span> T11 = Foo&lt;{ a: <span class="hljs-built_in">string</span>, b: <span class="hljs-built_in">number</span> }&gt;;  <span class="hljs-comment">// string | number</span>
</code></pre>
<p>&#x76F8;&#x4F3C;&#x5730;&#xFF0C;&#x5728;&#x6297;&#x53D8;&#x4F4D;&#x7F6E;&#x4E0A;&#xFF0C;&#x540C;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#x7684;&#x591A;&#x4E2A;&#x5019;&#x9009;&#x7C7B;&#x578B;&#x4F1A;&#x88AB;&#x63A8;&#x65AD;&#x4E3A;&#x4EA4;&#x53C9;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Bar&lt;T&gt; = T extends { a: (x: infer U) =&gt; <span class="hljs-built_in">void</span>, b: (x: infer U) =&gt; <span class="hljs-built_in">void</span> } ? U : never;
<span class="hljs-keyword">type</span> T20 = Bar&lt;{ a: (x: <span class="hljs-built_in">string</span>) =&gt; <span class="hljs-built_in">void</span>, b: (x: <span class="hljs-built_in">string</span>) =&gt; <span class="hljs-built_in">void</span> }&gt;;  <span class="hljs-comment">// string</span>
<span class="hljs-keyword">type</span> T21 = Bar&lt;{ a: (x: <span class="hljs-built_in">string</span>) =&gt; <span class="hljs-built_in">void</span>, b: (x: <span class="hljs-built_in">number</span>) =&gt; <span class="hljs-built_in">void</span> }&gt;;  <span class="hljs-comment">// string &amp; number</span>
</code></pre>
<p>&#x5F53;&#x63A8;&#x65AD;&#x5177;&#x6709;&#x591A;&#x4E2A;&#x8C03;&#x7528;&#x7B7E;&#x540D;&#xFF08;&#x4F8B;&#x5982;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#x7C7B;&#x578B;&#xFF09;&#x7684;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x7528;<em>&#x6700;&#x540E;</em>&#x7684;&#x7B7E;&#x540D;&#xFF08;&#x5927;&#x6982;&#x662F;&#x6700;&#x81EA;&#x7531;&#x7684;&#x5305;&#x542B;&#x6240;&#x6709;&#x60C5;&#x51B5;&#x7684;&#x7B7E;&#x540D;&#xFF09;&#x8FDB;&#x884C;&#x63A8;&#x65AD;&#x3002;
&#x65E0;&#x6CD5;&#x6839;&#x636E;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x5217;&#x8868;&#x6765;&#x89E3;&#x6790;&#x91CD;&#x8F7D;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">x: <span class="hljs-built_in">string</span></span>): <span class="hljs-title">number</span></span>;
<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">x: <span class="hljs-built_in">number</span></span>): <span class="hljs-title">string</span></span>;
<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">x: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span></span>): <span class="hljs-title">string</span> | <span class="hljs-title">number</span></span>;
<span class="hljs-keyword">type</span> T30 = ReturnType&lt;<span class="hljs-keyword">typeof</span> foo&gt;;  <span class="hljs-comment">// string | number</span>
</code></pre>
<p>&#x65E0;&#x6CD5;&#x5728;&#x6B63;&#x5E38;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x7684;&#x7EA6;&#x675F;&#x5B50;&#x8BED;&#x53E5;&#x4E2D;&#x4F7F;&#x7528;<code>infer</code>&#x58F0;&#x660E;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> ReturnType&lt;T extends (...args: <span class="hljs-built_in">any</span>[]) =&gt; infer R&gt; = R;  <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;&#x4E0D;&#x652F;&#x6301;</span>
</code></pre>
<p>&#x4F46;&#x662F;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x8FBE;&#x5230;&#x540C;&#x6837;&#x7684;&#x6548;&#x679C;&#xFF0C;&#x5728;&#x7EA6;&#x675F;&#x91CC;&#x5220;&#x6389;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#xFF0C;&#x7528;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#x66FF;&#x6362;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> AnyFunction = (...args: <span class="hljs-built_in">any</span>[]) =&gt; <span class="hljs-built_in">any</span>;
<span class="hljs-keyword">type</span> ReturnType&lt;T extends AnyFunction&gt; = T extends (...args: <span class="hljs-built_in">any</span>[]) =&gt; infer R ? R : <span class="hljs-built_in">any</span>;
</code></pre>
<h3 id="&#x9884;&#x5B9A;&#x4E49;&#x7684;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;">&#x9884;&#x5B9A;&#x4E49;&#x7684;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;</h3>
<p>TypeScript 2.8&#x5728;<code>lib.d.ts</code>&#x91CC;&#x589E;&#x52A0;&#x4E86;&#x4E00;&#x4E9B;&#x9884;&#x5B9A;&#x4E49;&#x7684;&#x6709;&#x6761;&#x4EF6;&#x7C7B;&#x578B;&#xFF1A;</p>
<ul>
<li><code>Exclude&lt;T, U&gt;</code> -- &#x4ECE;<code>T</code>&#x4E2D;&#x5254;&#x9664;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;<code>U</code>&#x7684;&#x7C7B;&#x578B;&#x3002;</li>
<li><code>Extract&lt;T, U&gt;</code> -- &#x63D0;&#x53D6;<code>T</code>&#x4E2D;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;<code>U</code>&#x7684;&#x7C7B;&#x578B;&#x3002;</li>
<li><code>NonNullable&lt;T&gt;</code> -- &#x4ECE;<code>T</code>&#x4E2D;&#x5254;&#x9664;<code>null</code>&#x548C;<code>undefined</code>&#x3002;</li>
<li><code>ReturnType&lt;T&gt;</code> -- &#x83B7;&#x53D6;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x3002;</li>
<li><code>InstanceType&lt;T&gt;</code> -- &#x83B7;&#x53D6;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x7C7B;&#x578B;&#x3002;</li>
</ul>
<h4 id="example">Example</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> T00 = Exclude&lt;<span class="hljs-string">&quot;a&quot;</span> | <span class="hljs-string">&quot;b&quot;</span> | <span class="hljs-string">&quot;c&quot;</span> | <span class="hljs-string">&quot;d&quot;</span>, <span class="hljs-string">&quot;a&quot;</span> | <span class="hljs-string">&quot;c&quot;</span> | <span class="hljs-string">&quot;f&quot;</span>&gt;;  <span class="hljs-comment">// &quot;b&quot; | &quot;d&quot;</span>
<span class="hljs-keyword">type</span> T01 = Extract&lt;<span class="hljs-string">&quot;a&quot;</span> | <span class="hljs-string">&quot;b&quot;</span> | <span class="hljs-string">&quot;c&quot;</span> | <span class="hljs-string">&quot;d&quot;</span>, <span class="hljs-string">&quot;a&quot;</span> | <span class="hljs-string">&quot;c&quot;</span> | <span class="hljs-string">&quot;f&quot;</span>&gt;;  <span class="hljs-comment">// &quot;a&quot; | &quot;c&quot;</span>

<span class="hljs-keyword">type</span> T02 = Exclude&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span> | (() =&gt; <span class="hljs-built_in">void</span>), <span class="hljs-built_in">Function</span>&gt;;  <span class="hljs-comment">// string | number</span>
<span class="hljs-keyword">type</span> T03 = Extract&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span> | (() =&gt; <span class="hljs-built_in">void</span>), <span class="hljs-built_in">Function</span>&gt;;  <span class="hljs-comment">// () =&gt; void</span>

<span class="hljs-keyword">type</span> T04 = NonNullable&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span> | <span class="hljs-literal">undefined</span>&gt;;  <span class="hljs-comment">// string | number</span>
<span class="hljs-keyword">type</span> T05 = NonNullable&lt;(() =&gt; <span class="hljs-built_in">string</span>) | <span class="hljs-built_in">string</span>[] | <span class="hljs-literal">null</span> | <span class="hljs-literal">undefined</span>&gt;;  <span class="hljs-comment">// (() =&gt; string) | string[]</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f1</span>(<span class="hljs-params">s: <span class="hljs-built_in">string</span></span>) </span>{
    <span class="hljs-keyword">return</span> { a: <span class="hljs-number">1</span>, b: s };
}

<span class="hljs-keyword">class</span> C {
    x = <span class="hljs-number">0</span>;
    y = <span class="hljs-number">0</span>;
}

<span class="hljs-keyword">type</span> T10 = ReturnType&lt;() =&gt; <span class="hljs-built_in">string</span>&gt;;  <span class="hljs-comment">// string</span>
<span class="hljs-keyword">type</span> T11 = ReturnType&lt;(s: <span class="hljs-built_in">string</span>) =&gt; <span class="hljs-built_in">void</span>&gt;;  <span class="hljs-comment">// void</span>
<span class="hljs-keyword">type</span> T12 = ReturnType&lt;(&lt;T&gt;() =&gt; T)&gt;;  <span class="hljs-comment">// {}</span>
<span class="hljs-keyword">type</span> T13 = ReturnType&lt;(&lt;T extends U, U extends <span class="hljs-built_in">number</span>[]&gt;() =&gt; T)&gt;;  <span class="hljs-comment">// number[]</span>
<span class="hljs-keyword">type</span> T14 = ReturnType&lt;<span class="hljs-keyword">typeof</span> f1&gt;;  <span class="hljs-comment">// { a: number, b: string }</span>
<span class="hljs-keyword">type</span> T15 = ReturnType&lt;<span class="hljs-built_in">any</span>&gt;;  <span class="hljs-comment">// any</span>
<span class="hljs-keyword">type</span> T16 = ReturnType&lt;never&gt;;  <span class="hljs-comment">// any</span>
<span class="hljs-keyword">type</span> T17 = ReturnType&lt;<span class="hljs-built_in">string</span>&gt;;  <span class="hljs-comment">// Error</span>
<span class="hljs-keyword">type</span> T18 = ReturnType&lt;<span class="hljs-built_in">Function</span>&gt;;  <span class="hljs-comment">// Error</span>

<span class="hljs-keyword">type</span> T20 = InstanceType&lt;<span class="hljs-keyword">typeof</span> C&gt;;  <span class="hljs-comment">// C</span>
<span class="hljs-keyword">type</span> T21 = InstanceType&lt;<span class="hljs-built_in">any</span>&gt;;  <span class="hljs-comment">// any</span>
<span class="hljs-keyword">type</span> T22 = InstanceType&lt;never&gt;;  <span class="hljs-comment">// any</span>
<span class="hljs-keyword">type</span> T23 = InstanceType&lt;<span class="hljs-built_in">string</span>&gt;;  <span class="hljs-comment">// Error</span>
<span class="hljs-keyword">type</span> T24 = InstanceType&lt;<span class="hljs-built_in">Function</span>&gt;;  <span class="hljs-comment">// Error</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;<code>Exclude</code>&#x7C7B;&#x578B;&#x662F;<a href="https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458" target="_blank">&#x5EFA;&#x8BAE;&#x7684;</a><code>Diff</code>&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x79CD;&#x5B9E;&#x73B0;&#x3002;&#x6211;&#x4EEC;&#x4F7F;&#x7528;<code>Exclude</code>&#x8FD9;&#x4E2A;&#x540D;&#x5B57;&#x662F;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x7834;&#x574F;&#x5DF2;&#x7ECF;&#x5B9A;&#x4E49;&#x4E86;<code>Diff</code>&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5E76;&#x4E14;&#x6211;&#x4EEC;&#x611F;&#x89C9;&#x8FD9;&#x4E2A;&#x540D;&#x5B57;&#x80FD;&#x66F4;&#x597D;&#x5730;&#x8868;&#x8FBE;&#x7C7B;&#x578B;&#x7684;&#x8BED;&#x4E49;&#x3002;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x589E;&#x52A0;<code>Omit&lt;T, K&gt;</code>&#x7C7B;&#x578B;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x53EF;&#x4EE5;&#x5F88;&#x5BB9;&#x6613;&#x7684;&#x7528;<code>Pick&lt;T, Exclude&lt;keyof T, K&gt;&gt;</code>&#x6765;&#x8868;&#x793A;&#x3002;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Type Compatibility.html" class="navigation navigation-prev " aria-label="Previous page: 类型兼容性">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Utilit Types.md" class="navigation navigation-next " aria-label="Next page: 实用工具类型">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"高级类型","level":"1.3.10","depth":2,"next":{"title":"实用工具类型","level":"1.3.11","depth":2,"path":"doc/handbook/Utilit Types.md","ref":"./doc/handbook/Utilit Types.md","articles":[]},"previous":{"title":"类型兼容性","level":"1.3.9","depth":2,"path":"doc/handbook/Type Compatibility.md","ref":"./doc/handbook/Type Compatibility.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Patrick Zhong","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"preface.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"TypeScript Handbook","language":"zh-hans","gitbook":">3.0.0","description":"Translation of official TypeScript Handbook"},"file":{"path":"doc/handbook/Advanced Types.md","mtime":"2018-10-13T09:48:54.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-13T09:57:42.904Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

